How to get file name when user select a file via <input type=“file” />?
来源:互联网 发布:镇江淘宝模特兼职招聘 编辑:程序博客网 时间:2024/05/22 10:48
I've seen similar questions before,which ends up with no solution,because of security reasons.
But today I see hostmonster has successfully implemented this,when I open a ticket and attach a file in their backend.
It works both with firefox and IE(version 8 to be exact).
I've verified it's exactly client side scripting,no request is sent(with firebug).
Now,can we re-consider this question?
2 Answers
You can get the file name, but you cannot get the full client file-system path.
Try to access to the value
attribute of your file input
on the change
event.
Most browsers will give you only the file name, but there are exceptions like IE8 which will give you afake path like: "C:\fakepath\myfile.ext"
and older versions (IE <= 6) which actually will give you the full client file-system path (due its lack of security).
document.getElementById('fileInput').onchange = function () { alert('Selected file: ' + this.value);};
C:\fakepath\...
in chrome, but it's fine for my purposes. Thanks. – Squirrl Mar 3 '14 at 1:05f = f.replace(/.*[\/\\]/, '');
– Sam Watkins Jul 20 at 9:34just tested doing this and it seems to work in firefox & IE
<html> <head> <script type="text/javascript"> function alertFilename() { var thefile = document.getElementById('thefile'); alert(thefile.value); } </script> </head> <body> <form> <input type="file" id="thefile" onchange="alertFilename()" /> <input type="button" onclick="alertFilename()" value="alert" /> </form> </body></html>
I've seen similar questions before,which ends up with no solution,because of security reasons.
But today I see hostmonster has successfully implemented this,when I open a ticket and attach a file in their backend.
It works both with firefox and IE(version 8 to be exact).
I've verified it's exactly client side scripting,no request is sent(with firebug).
Now,can we re-consider this question?
2 Answers
You can get the file name, but you cannot get the full client file-system path.
Try to access to the value
attribute of your file input
on the change
event.
Most browsers will give you only the file name, but there are exceptions like IE8 which will give you afake path like: "C:\fakepath\myfile.ext"
and older versions (IE <= 6) which actually will give you the full client file-system path (due its lack of security).
document.getElementById('fileInput').onchange = function () { alert('Selected file: ' + this.value);};
C:\fakepath\...
in chrome, but it's fine for my purposes. Thanks. – Squirrl Mar 3 '14 at 1:05f = f.replace(/.*[\/\\]/, '');
– Sam Watkins Jul 20 at 9:34just tested doing this and it seems to work in firefox & IE
<html> <head> <script type="text/javascript"> function alertFilename() { var thefile = document.getElementById('thefile'); alert(thefile.value); } </script> </head> <body> <form> <input type="file" id="thefile" onchange="alertFilename()" /> <input type="button" onclick="alertFilename()" value="alert" /> </form> </body></html>