在ie6中对于<input type="file" />通过obj.value是可以获取客户端选择文件的全路径的,但是到ie7就只能获取文件名,这对于onchange事件立即显示图片会有问题,可以用js方法解决
具体代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1 <html>
2 <head>
3 <title>get file input full path</title>
4 <script language='javascript'>
5 function getFullPath(obj)
6 {
7 if(obj)
8 {
9 //ie
10 if (window.navigator.userAgent.indexOf("MSIE")>=1)
11 {
12 obj.select();
13 return document.selection.createRange().text;
14 }
15 //firefox
16 else if(window.navigator.userAgent.indexOf("Firefox")>=1)
17 {
18 if(obj.files)
19 {
20 return obj.files.item(0).getAsDataURL();
21 }
22 return obj.value;
23 }
24 return obj.value;
25 }
26 }
27 </script>
28 </head>
29 <body>
30 <input type="file" onchange="document.getElementById('img').src=getFullPath(this);" />
31 <img id="img" />
32 </body>
33 </html>