Use jQuery to get the file input's selected filename without the path

后端 未结 14 684
醉话见心
醉话见心 2020-11-27 10:02

I used this:

$(\'input[type=file]\').val()

to get the file name selected, but it returned the full path, as in \"C:\\fakepath\\filename.doc

相关标签:
14条回答
  • 2020-11-27 11:00
    var filename = $('input[type=file]').val().split('\\').pop();
    

    or you could just do (because it's always C:\fakepath that is added for security reasons):

    var filename = $('input[type=file]').val().replace(/C:\\fakepath\\/i, '')
    
    0 讨论(0)
  • 2020-11-27 11:01

    Get the first file from the control and then get the name of the file, it will ignore the file path on Chrome, and also will make correction of path for IE browsers. On saving the file, you have to use System.io.Path.GetFileName method to get the file name only for IE browsers

    var fileUpload    = $("#ContentPlaceHolder1_FileUpload_mediaFile").get(0); 
    var files         =  fileUpload.files; 
    var mediafilename = ""; 
    
    for (var i = 0; i < files.length; i++) { 
      mediafilename = files[i].name; 
    } 
    
    0 讨论(0)
提交回复
热议问题