user937450 user937450 - 1 year ago 72
HTML Question

Get only file name from file input on internet explorer

I need to return only the file name from an HTML input file.

<input type="file" id="whatever" />

The JavaScript code im using to get the name of the file is:


In firefox it gives only the file name which is what I want, but in IE I get the full path.

I think that string manipulation is the only way to get the name.

What would be the easiest/shortest way to get only the name (extension too) in JavaScript? Thanks.

Answer Source

You can try this

var path = document.getElementById("whatever").value;
var fileName = path.match(/[^\/\\]+$/);