How to get file name when user select a file via <input type=“file” />?

来源:互联网 发布:镇江淘宝模特兼职招聘 编辑:程序博客网 时间:2024/05/22 10:48


14down votefavorite
3

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?

shareimprove this question
 
 
What do you mean by "get a file name"? Isn't the file name obvious, because the user just selected it? – John Feminella Feb 3 '10 at 4:11

2 Answers

activeoldestvotes
up vote27down voteaccepted

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);};
shareimprove this answer
 
1 
I'm getting C:\fakepath\... in chrome, but it's fine for my purposes. Thanks. –  Squirrl Mar 3 '14 at 1:05
 
This is about security reason. Web sites must not learn user's folder path. –  kodmanyagha Jan 24 at 10:33
 
Can clean it up like this: f = f.replace(/.*[\/\\]/, ''); –  Sam Watkins Jul 20 at 9:34
up vote4down vote

just 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>
14down votefavorite
3

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?

shareimprove this question
 
   
What do you mean by "get a file name"? Isn't the file name obvious, because the user just selected it? – John Feminella Feb 3 '10 at 4:11

2 Answers

activeoldestvotes
up vote27down voteaccepted

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);};
shareimprove this answer
 
1 
I'm getting C:\fakepath\... in chrome, but it's fine for my purposes. Thanks. –  Squirrl Mar 3 '14 at 1:05
   
This is about security reason. Web sites must not learn user's folder path. –  kodmanyagha Jan 24 at 10:33
   
Can clean it up like this: f = f.replace(/.*[\/\\]/, ''); –  Sam Watkins Jul 20 at 9:34
up vote4down vote

just 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>
0 0
原创粉丝点击