172

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

1
  • 11
    The browser changes the real path to C:\fakepath\ so malicious sites can't use javascript to glean information about your computer's directory structure. Commented Jun 16, 2011 at 0:22

14 Answers 14

335
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, '') 
Sign up to request clarification or add additional context in comments.

9 Comments

Exactly what I needed, manji! I need to check the extension, too, so I used your example this way: var extension = filename.split('.').pop(); to get that, too! Thanks!
Is it Fakepath with a capital F?
@MikeDeSimone I've tested split('\\').pop(); on Win 7, Ubuntu 11.04 and Mac OS X and it works fine on all of them.
@Alex: Wow, that's pretty crazy. So it's really just a workaround for someone's bad JS code from way back that will never get fixed. I'm running Safari on a Mac, and I see "C:\fakepath\" in there. (Play with it in jsfiddle.)
@VítorBaptista Windows files cannot have backslashes in their names. It is possible on MacOS although "You should avoid using colons and slashes in the names of files and folders because some operating systems and drive formats use these characters as directory separators" as described in Apple's website
|
76

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 : ""; 

2 Comments

One thing to consider when using this option: when you click again on 'Choose file' button to bring up the open file window, then click on the escape button, a console error will be thrown.
Yep, you are right, but the intention was putting just the code to get the name because as the code are getting the name from a hard coded position it would be necessary check if there is some file (an for this case we are talking in only one file, but could have more and the code would need to iterate by all elements). But sure, I'm going to update the code and include this validation.
29

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 

2 Comments

The example code var filename = $('input[type=file]').val().replace(/.*(\/|\\)/, ''); is using jQuery.
@Zarepheth I think we can apply a similar pattern when using other libraries.
23

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.

Comments

16

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 

1 Comment

Since the field ID does nothing in this context, The function in JS should have 1 parameter less, just e instead of fileSelect(id,e) just fileSelect(e). Thanks though for the solution :).
9

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

Comments

9

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.'); }); 

Comments

5

How about something like this?

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

Comments

4

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

Comments

4

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

Comments

4
<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> 

Comments

4

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> 

Comments

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

Comments

0

We can also remove it using match

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.