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

JavascriptJqueryGetFilenames

Javascript Problem Overview


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)?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

You just need to do the code below. The first [0] is to access the HTML element and second [0] is to access the first file of the file upload (I included a validation in case that there is no file):

    var filename = $('input[type=file]')[0].files.length ? ('input[type=file]')[0].files[0].name : "";

Solution 3 - Javascript

Get path work with all OS

var filename = $('input[type=file]').val().replace(/.*(\/|\\)/, '');

Example

C:\fakepath\filename.doc 
/var/fakepath/filename.doc

Both return

filename.doc
filename.doc

Solution 4 - Javascript

Chrome returns C:\fakepath\... for security reasons - a website should not be able to obtain information about your computer such as the path to a file on your computer.

To get just the filename portion of a string, you can use split()...

var file = path.split('\\').pop();

jsFiddle.

...or a regular expression...

var file = path.match(/\\([^\\]+)$/)[1];

jsFiddle.

...or lastIndexOf()...

var file = path.substr(path.lastIndexOf('\\') + 1);

jsFiddle.

Solution 5 - Javascript

Here is how I do it, it works pretty well.

In your HTML do:

<input type="file" name="Att_AttributeID" onchange="fileSelect(event)" class="inputField" />

Then in your js file create a simple function:

function fileSelect(id, e){
    console.log(e.target.files[0].name);
}

If you're doing multiple files, you should also be able to get the list by looping over this:

e.target.files[0].name

Solution 6 - Javascript

maybe some addition for avoid fakepath:

var fileName = $('input[type=file]').val();
var clean=fileName.split('\\').pop(); // clean from C:\fakepath OR C:\fake_path 
alert('clean file name : '+ fileName);

Solution 7 - Javascript

How about something like this?

var pathArray = $('input[type=file]').val().split('\\');
alert(pathArray[pathArray.length - 1]);

Solution 8 - Javascript

Does it have to be jquery? Or can you just use JavaScript's native yourpath.split("\\") to split the string to an array?

Solution 9 - Javascript

<script type="text/javascript">

    $('#upload').on('change',function(){
       // output raw value of file input
       $('#filename').html($(this).val().replace(/.*(\/|\\)/, '')); 

        // or, manipulate it further with regex etc.
        var filename = $(this).val().replace(/.*(\/|\\)/, '');
        // .. do your magic

        $('#filename').html(filename);
    });
</script>

Solution 10 - Javascript

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; 
} 

Solution 11 - Javascript

Here you can call like this Let this is my Input File control

  <input type="file" title="search image" id="file" name="file" onchange="show(this)"  />

Now here is my Jquery which get called once you select the file

<script type="text/javascript">
    function show(input) {
        var fileName = input.files[0].name;
        alert('The file "' + fileName + '" has been selected.');               
            }
        
</script>

Solution 12 - Javascript

This alternative seems the most appropriate.

$('input[type="file"]').change(function(e){
        var fileName = e.target.files[0].name;
        alert('The file "' + fileName +  '" has been selected.');
});

Solution 13 - Javascript

var filename=location.href.substr(location.href.lastIndexOf("/")+1);
alert(filename);

Solution 14 - Javascript

We can also remove it using match

var fileName = $('input:file').val().match(/[^\\/]*$/)[0];
$('#file-name').val(fileName);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarkyView Question on Stackoverflow
Solution 1 - JavascriptmanjiView Answer on Stackoverflow
Solution 2 - JavascriptDiego CotiniView Answer on Stackoverflow
Solution 3 - JavascriptKotzillaView Answer on Stackoverflow
Solution 4 - JavascriptalexView Answer on Stackoverflow
Solution 5 - JavascriptbartlssView Answer on Stackoverflow
Solution 6 - JavascriptvafrcorView Answer on Stackoverflow
Solution 7 - JavascriptBertrand MarronView Answer on Stackoverflow
Solution 8 - JavascriptGolezTrolView Answer on Stackoverflow
Solution 9 - Javascriptreal vishalView Answer on Stackoverflow
Solution 10 - JavascriptAzhar ShahazadView Answer on Stackoverflow
Solution 11 - JavascriptDebendra DashView Answer on Stackoverflow
Solution 12 - JavascriptFischer TiradoView Answer on Stackoverflow
Solution 13 - JavascriptMehul RojasaraView Answer on Stackoverflow
Solution 14 - JavascriptTammyView Answer on Stackoverflow