惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
I
Intezer
Security Latest
Security Latest
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
O
OpenAI News
A
Arctic Wolf
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
M
MIT News - Artificial intelligence
F
Full Disclosure
P
Privacy International News Feed
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Hacker News: Front Page
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
Cloudbric
Cloudbric
大猫的无限游戏
大猫的无限游戏
Google Online Security Blog
Google Online Security Blog
Recent Announcements
Recent Announcements
H
Help Net Security
量子位
V
V2EX
美团技术团队
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
V2EX - 技术
V2EX - 技术
D
Docker
博客园 - 【当耐特】
Project Zero
Project Zero
博客园 - 司徒正美

博客园 - 蓝蓝的天2016

学习materialize VS2013 启用avalon 智能提示 Intelligence 推荐一款开源的C#TCP通讯框架 HTML5的 input:file上传类型控制(转载) tinymce4.x 上传本地图片 (转载) Jquery append()总结(一) 转载 Visual Studio各版本工程文件之间的转换 [转载] 创建数据库表(转载) 用IIS防止mdb数据库被下载(转载) 放映PPT幻灯片演示文稿如何让演讲者备注不投影到屏幕上(转载) 操作App.config的类(转载) C# MemoryCache 类[转载] 转载css3 图片圆形显示 如何CSS将正方形图片显示为圆形图片布局 微信 学习网址 winform 路径 CodeSimth - .Net Framework Data Provider 可能没有安装。解决方法[转载 ] 加载js代码 tinymce 上传图片空间(转) 页面跳转
js/jquery 获取本地文件的文件路劲 获取input框中type=‘file’ 中的文件路径(转载)
蓝蓝的天2016 · 2016-11-01 · via 博客园 - 蓝蓝的天2016

 原文:http://blog.csdn.net/niyingxunzong/article/details/16989947

js/jquery 获取本地文件的文件路劲 获取input框中type=‘file’ 中的文件路径

2013-11-27 18:38 14483人阅读 

分为两部分,自己去判断浏览器的类型,然后调用不同函数,一定要引入jQuery,上面是我的Jquery的路径

在IE低版本中可以直接获得文件路径,不过在高版本和firefox和chrome中是不允许的。那是个漏洞

这样就能实现不用上传就可以实现图片的实时预览了

1.IE内核的部分,IE10 没问题,别的没试,

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>无标题文档</title>  
  6.   
  7.   
  8. <script type="text/javascript" src="软件工程概论/软件工程实验原型/js/jquery-1.8.3.min.js"></script>  
  9. <script type="text/javascript">  
  10.   
  11. var imgurl = "";  
  12.   
  13. function getImgURL(node) {    
  14.     var imgURL = "";      
  15.     var file = null;  
  16.     if(node.files && node.files[0] ){  
  17.         file = node.files[0];   
  18.     }else if(node.files && node.files.item(0)) {                                  
  19.         file = node.files.item(0);     
  20.     }     
  21.       
  22.     //这种获取方式支持IE10    
  23.     node.select();    
  24.     imgURL = document.selection.createRange().text;      
  25.     alert(imgURL);  
  26.       
  27.       
  28.     var textHtml = "<img src='"+imgURL+"'/>";     //创建img标签用于显示图片  
  29.     alert(textHtml);  
  30.     $(".mark").after(textHtml);  
  31.     return imgURL;  
  32. }  
  33.          
  34. </script>  
  35.   
  36. </head>  
  37.   
  38. <body>  
  39.     <div style="width:200px; height:210px; border:1px solid red;" id="show">  
  40.         <div class="mark"></div>  
  41.     </div>  
  42. <br>  
  43. <input type="file" value="上传文件" onchange="getImgURL(this)">  
  44. </body>  
  45. </html>  

2.火狐和chrome浏览器,其实这个获得的文件路径不是我们能看懂的,它是一个对象,不过浏览器能解析,可能出于浏览器的安全考虑吧,本来不能显示文件路径

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>无标题文档</title>  
  6.   
  7. <script type="text/javascript" src="软件工程概论/软件工程实验原型/js/jquery-1.8.3.min.js"></script>  
  8. <script type="text/javascript">  
  9.   
  10. var imgurl = "";  
  11.   
  12. function getImgURL(node) {    
  13.     var imgURL = "";      
  14.     try{     
  15.         var file = null;  
  16.         if(node.files && node.files[0] ){  
  17.             file = node.files[0];   
  18.         }else if(node.files && node.files.item(0)) {                                  
  19.             file = node.files.item(0);     
  20.         }     
  21.         //Firefox 因安全性问题已无法直接通过input[file].value 获取完整的文件路径  
  22.         try{  
  23.             //Firefox7.0   
  24.             imgURL =  file.getAsDataURL();    
  25.             //alert("//Firefox7.0"+imgRUL);                           
  26.         }catch(e){  
  27.             //Firefox8.0以上                                
  28.             imgRUL = window.URL.createObjectURL(file);  
  29.             //alert("//Firefox8.0以上"+imgRUL);  
  30.         }  
  31.      }catch(e){      //这里不知道怎么处理了,如果是遨游的话会报这个异常                   
  32.         //支持html5的浏览器,比如高版本的firefox、chrome、ie10  
  33.         if (node.files && node.files[0]) {                            
  34.             var reader = new FileReader();   
  35.             reader.onload = function (e) {                                        
  36.                 imgURL = e.target.result;    
  37.             };  
  38.             reader.readAsDataURL(node.files[0]);   
  39.         }    
  40.      }  
  41.     //imgurl = imgURL;  
  42.     creatImg(imgRUL);  
  43.     return imgURL;  
  44. }  
  45.          
  46. function creatImg(imgRUL){   //根据指定URL创建一个Img对象  
  47.     var textHtml = "<img src='"+imgRUL+"'/>";  
  48.     $(".mark").after(textHtml);  
  49. }  
  50.          
  51. </script>  
  52.   
  53. </head>  
  54.   
  55. <body>  
  56.     <div style="width:90px; height:110px; overflow:hidden; border:1px solid red;" id="show">  
  57.         <div class="mark"></div>  
  58.     </div>  
  59. <br>  
  60. <input type="file" value="上传文件" onchange="getImgURL(this)">  
  61. </body>  
  62. </html>  

3.其余的浏览器。我没有测试,不过国内的其他如360和遨游,等都有两种模式,一种是IE内核,这(1)中可以运行,第二种内核没找到好方法

4.推荐出处

https://developer.mozilla.org/zh-CN/docs/DOM

http://www.w3.org/TR/FileAPI/#dfn-revokeObjectURL

https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Example.3A_Using_object_URLs_to_display_images