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:
document.getElementById("whatever").value;
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.
You can try this
var path = document.getElementById("whatever").value;
var fileName = path.match(/[^\/\\]+$/);
console.log(fileName);
I hope this works.
var fullFileName = document.getElementById("whatever").value;
var fileName = fullFileName.substr(fullFileName.lastIndexOf("\\")+1, fullFileName.length);
Here is the fiddle for that Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With