Search code examples
javascriptjquerygetfilenames

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


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". The "fakepath" part was actually there - not sure if it's supposed to be, but this is my first time working with the filename of file uploads.

How can I just get the file name (filename.doc)?


Solution

  • 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, '')