I need to return only the file name from an HTML input file.
The JavaScript code im using to get
You can try this
var path = document.getElementById("whatever").value;
var fileName = path.match(/[^\/\\]+$/);
console.log(fileName);
var path = document.getElementById("whatever").value;
var filename = path.substring(path.lastIndexOf("/") + 1);
This will give you everything after the last /
, but is also compatible with a lack of /
s. In case you also need to deal with \
as the lovely path separator, you can always use this first:
path.replace(/\\/g, "/")
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