Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  197] [ 1]  / answers: 1 / hits: 19431  / 11 Years ago, tue, april 9, 2013, 12:00:00

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.


More From » html

 Answers
43

You can try this



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

[#79030] Monday, April 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jalyn

Total Points: 173
Total Questions: 96
Total Answers: 90

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;