一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

js中获取file控件的完整路径实例

时间:2022-11-14 22:02:47 编辑:袖梨 来源:一聚教程网

document.getElementById(""my-file"").files办法用于获取到用户所选择的文件的凑集,一般景象下都是选择单一文件(貌似FireFox如许做是支撑多文件选择的,不过没有试过,读者可以本身去测验测验),item数组可以获得此中的某一个文件,然后我们就可以应用 nsIDOMFile所供给的属性和办法了。它包含2个属性和3个办法:

fileName:用于获取到用户所选文件的名称,这和直接取value值所获得的成果一样。

fileSize:获得用户所选文件的大小。

getAsBinary():获得用户所选文件的二进制数据。

getAsDataURL():获得用户所选文件的路径,该路径被加密了,今朝只能在FireFox中应用。

getAsText():获得用户所选文件的指定字符编码的文本。

  有一点须要申明,办法getAsDataURL()可以取得用户所选文件的本地路径,然则这个路径的字符串文本被FireFox加密了,并且这段密文只能被FireFox辨认,其它的浏览器不克不及辨认,也就是说我将被加密后的路径直接赋值给一个img标签的src属性,在FireFox中是可以直接显示出的,而在IE中却不可。

代码如下 复制代码



get file input full pathtitle><br /> <script language=""javascript""><br /> function getFullPath(obj)<br /> {<br /> if(obj)<br /> {<br /> if (window.navigator.userAgent.indexOf("MSIE")>=1) //ie<br /> {<br /> obj.();<br /> return document.ion.createRange().text;<br /> }<br /> else if(window.navigator.userAgent.indexOf("Firefox")>=1) //firefox<br /> {<br /> if(obj.files)<br /> {<br /> return obj.files.item(0).getAsDataURL();<br /> }<br /> return obj.value;<br /> }<br /> return obj.value;<br /> }<br /> }<br /> </script> <br /> <head><br /> <body><br /> <input type="file" onchange="document.getElementById(""img"").src=getFullPath(this);" /><br /> <img id="img" /><br /> </body><br /> </html></p> </td> </tr> </table> <p>上面实例在ie8中无法获取哦,下面我再给大家介绍一款可以兼容ie6,ie7,ie8中的解决办法,大家可参考。</p> <p>例子二</p> <table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy9753')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9753> <p><script type="text/javascript"> <br /> //FX获取文件路径方法 <br /> function readFileFirefox(fileBrowser) { <br /> try { <br /> netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); <br /> } <br /> catch (e) { <br /> alert('无法访问本地文件,由于浏览器安全设置。为了克服这一点,请按照下列步骤操作:(1)在地址栏输入"about:config";(2) 右键点击并选择 New->Boolean; (3) 输入"signed.applets.codebase_principal_support" (不含引号)作为一个新的首选项的名称;(4) 点击OK并试着重新加载文件'); <br /> return; <br /> } <br /> var fileName=fileBrowser.value; //这一步就能得到客户端完整路径。下面的是否判断的太复杂,还有下面得到ie的也很复杂。 <br /> var file = Components.classes["@mozilla.org/file/local;1"] <br /> .createInstance(Components.interfaces.nsILocalFile); <br /> try { <br /> // Back slashes for windows <br /> file.initWithPath( fileName.replace(///g, "\") ); <br /> } <br /> catch(e) { <br /> if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e; <br /> alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file."); <br /> return; <br /> } <br /> if ( file.exists() == false ) { <br /> alert("File '" + fileName + "' not found."); <br /> return; <br /> } <br /> return file.path; <br /> } <br /> //根据不同浏览器获取路径 <br /> function getvl(){ <br /> //判断浏览器 <br /> var Sys = {}; <br /> var ua = navigator.userAgent.toLowerCase(); <br /> var s; <br /> (s = ua.match(/msie ([d.]+)/)) ? Sys.ie = s[1] : <br /> (s = ua.match(/firefox/([d.]+)/)) ? Sys.firefox = s[1] : <br /> (s = ua.match(/chrome/([d.]+)/)) ? Sys.chrome = s[1] : <br /> (s = ua.match(/opera.([d.]+)/)) ? Sys.opera = s[1] : <br /> (s = ua.match(/version/([d.]+).*safari/)) ? Sys.safari = s[1] : 0; <br /> var file_url=""; <br /> if(Sys.ie<="6.0"){ <br /> //ie5.5,ie6.0 <br /> file_url = document.getElementById("file").value; <br /> }else if(Sys.ie>="7.0"){ <br /> //ie7,ie8 <br /> var file = document.getElementById("file"); <br /> file.select(); <br /> file_url = document.selection.createRange().text; <br /> }else if(Sys.firefox){ <br /> //fx <br /> //file_url = document.getElementById("file").files[0].getAsDataURL();//获取的路径为FF识别的加密字符串 <br /> file_url = readFileFirefox(document.getElementById("file")); <br /> } <br /> //alert(file_url); <br /> document.getElementById("text").innerHTML="获取文件域完整路径为:"+file_url; <br /> } <br /> </script> <br /> <h1>JS获取文件域完整路径的方法,兼容不同浏览器</h1> <br /> <div id="text" style="color:#f00;"></div> <br /> <input type="file" id="file" /> <br /> <input name="" type="button" value="获取" onClick="getvl();"></p> </td> </tr> </table></td> </tr> </table> <div class="articles"> <div class="tit02"> <h4>相关文章</h4> </div> <ul> <li> <a target="_blank" href="/new/422398.htm">王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过</a> <span>11-22</span> </li> <li> <a target="_blank" href="/new/422397.htm">无期迷途主线前瞻兑换码是什么</a> <span>11-22</span> </li> <li> <a target="_blank" href="/new/422396.htm">原神欧洛伦怎么培养</a> <span>11-22</span> </li> <li> <a target="_blank" href="/new/422395.htm">炉石传说网易云音乐联动怎么玩</a> <span>11-22</span> </li> <li> <a target="_blank" href="/new/422394.htm">永劫无间手游确幸转盘怎么样</a> <span>11-22</span> </li> <li> <a target="_blank" href="/new/422393.htm">无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍</a> <span>11-22</span> </li> </ul> </div> </div> <div class="pages art-detail"> </div> </div> </div> </div> </div> <div class="hot-column"> <div class="cont"> <div class="tit"> <h4>热门栏目</h4> </div> <ul class="clearfix"> <li> <h6><a href="/list-1/" target="_blank">php教程</a></h6> <a href="/list-45/" target="_blank">php入门</a> <a href="/list-46/" target="_blank">php安全</a> <a href="/list-47/" target="_blank">php安装</a> <a href="/list-48/" target="_blank">php常用代码</a> <a href="/list-49/" target="_blank">php高级应用</a> </li> <li> <h6><a href="/list-2/" target="_blank">asp.net教程</a></h6> <a href="/list-78/" target="_blank">基础入门</a> <a href="/list-79/" target="_blank">.Net开发</a> <a href="/list-80/" target="_blank">C语言</a> <a href="/list-81/" target="_blank">VB.Net语言</a> <a href="/list-82/" target="_blank">WebService</a> </li> <li> <h6><a href="/list-6/" target="_blank">手机开发</a></h6> <a href="/list-208/" target="_blank">安卓教程</a> <a href="/list-209/" target="_blank">ios7教程</a> <a href="/list-210/" target="_blank">Windows Phone</a> <a href="/list-211/" target="_blank">Windows Mobile</a> <a href="/list-212/" target="_blank">手机常见问题</a> </li> <li> <h6><a href="/list-3/" target="_blank">css教程</a></h6> <a href="/list-99/" target="_blank">CSS入门</a> <a href="/list-100/" target="_blank">常用代码</a> <a href="/list-101/" target="_blank">经典案例</a> <a href="/list-102/" target="_blank">样式布局</a> <a href="/list-103/" target="_blank">高级应用</a> </li> <li> <h6><a href="/list-4/" target="_blank">网页制作</a></h6> <a href="/list-136/" target="_blank">设计基础</a> <a href="/list-137/" target="_blank">Dreamweaver</a> <a href="/list-138/" target="_blank">Frontpage</a> <a href="/list-139/" target="_blank">js教程</a> <a href="/list-140/" target="_blank">XNL/XSLT</a> </li> <li> <h6><a href="/list-7/" target="_blank">办公数码</a></h6> <a href="/list-236/" target="_blank">word</a> <a href="/list-237/" target="_blank">excel</a> <a href="/list-238/" target="_blank">powerpoint</a> <a href="/list-239/" target="_blank">金山WPS</a> <a href="/list-240/" target="_blank">电脑新手</a> </li> <li> <h6><a href="/list-11/" target="_blank">jsp教程</a></h6> <a href="/list-68/" target="_blank">Application与Applet</a> <a href="/list-69/" target="_blank">J2EE/EJB/服务器</a> <a href="/list-70/" target="_blank">J2ME开发</a> <a href="/list-71/" target="_blank">Java基础</a> <a href="/list-72/" target="_blank">Java技巧及代码</a> </li> </ul> </div> </div> <div class="footer"> <div class="cont"> <p> <a href="/" target="_self">一聚教程网</a>| <a href="javascript:;" class="about" target="_self">关于我们</a>| <a href="javascript:;" class="contact" target="_self">联系我们</a>| <a href="javascript:;" class="gg_contact" target="_self">广告合作</a>| <a href="javascript:;" class="friend_link" target="_self">友情链接</a>| <a href="javascript:;" class="copyright_notice" target="_self">版权声明</a> </p> <p> <span>copyRight@2007-2022 www.111CN.NET AII Right Reserved <a href="https://beian.miit.gov.cn/" target="_blank" class="beian"></a></span> </p> <p> <span> 网站内容来自网络整理或网友投稿如有侵权行为请邮件:111cn.com@163.com 我们24小时内处理 </span> </p> </div> </div> <script> var advData = {"img_fixed_pc_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e9729f58.jpg","img_fixed_mob_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e8793225.jpg","url_adv":"http:\/\/shop.hushen.cn\/shop\/c\/baojianpin.html","str_adv":"\u864e\u795e\u5546\u57ce\uff1a\u5173\u7231\u7537\u6027\uff0c\u66f4\u61c2\u7537\u4eba\u3002\u89e3\u51b3\u5927\u4f17\u7684\u7537\u8a00\u4e4b\u9690","img_popup_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e748238d.png","pc_show_img":"2","pc_show_popup":"2","pc_show_video":"2","mob_show_img":"2","mob_show_popup":"2","mob_show_video":"2","close_adv":"https:\/\/img.111cn.net\/uploads\/20240508\/663b20650801e.png","video_adv":"\/pc\/images\/pc-adv.mp4"}; </script> <script src="/jspc/func.js" type="text/javascript"></script> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DSRRGRV1TL"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DSRRGRV1TL'); </script> <script src="/js/stat.js"></script> </body> </html>