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

作者:袖梨 2022-11-14

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> </div> </section> <section class="wrap-box"> <div class="g-tit"> <h2>相关文章</h2> </div> <ul class="s-list nobord notop"> <li> <a href="/art-422398.htm" class="s-card"> <div class="s-card-l"> <p class="tit">王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-22</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241122/logo_67405dac33a761.jpeg" alt="王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过" /> </div> </a> </li> <li> <a href="/art-422397.htm" class="s-card"> <div class="s-card-l"> <p class="tit">无期迷途主线前瞻兑换码是什么</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-22</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241122/logo_67405633dab531.jpeg" alt="无期迷途主线前瞻兑换码是什么" /> </div> </a> </li> <li> <a href="/art-422396.htm" class="s-card"> <div class="s-card-l"> <p class="tit">原神欧洛伦怎么培养</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-22</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241122/logo_6740563012ddf1.jpeg" alt="原神欧洛伦怎么培养" /> </div> </a> </li> <li> <a href="/art-422395.htm" class="s-card"> <div class="s-card-l"> <p class="tit">炉石传说网易云音乐联动怎么玩</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-22</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241122/logo_6740562ef23031.jpeg" alt="炉石传说网易云音乐联动怎么玩" /> </div> </a> </li> <li> <a href="/art-422394.htm" class="s-card"> <div class="s-card-l"> <p class="tit">永劫无间手游确幸转盘怎么样</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-22</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241122/logo_6740562c720271.jpeg" alt="永劫无间手游确幸转盘怎么样" /> </div> </a> </li> <li> <a href="/art-422393.htm" class="s-card"> <div class="s-card-l"> <p class="tit">无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-22</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241122/logo_67404f99a9d8f1.jpeg" alt="无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍" /> </div> </a> </li> </ul> </section> <section class="wrap-box"> <div class="g-tit"> <h2>精彩推荐</h2> </div> <ul class="card-box"> <li class="card3"> <a href="/app/99428.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/671258fc7c56f.png" alt="沙威玛传奇安卓版" /> </div> <p class="figure-head">沙威玛传奇安卓版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99427.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711fc29100e3.png" alt="战盟大乱斗最新版" /> </div> <p class="figure-head">战盟大乱斗最新版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99426.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ef93e4d90.png" alt="大侠立志传最新版" /> </div> <p class="figure-head">大侠立志传最新版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99425.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ca5ba8572.png" alt="苍翼混沌效应最新版" /> </div> <p class="figure-head">苍翼混沌效应最新版</p> <span class="figure-btn">下载</span> </a> </li> </ul> <ul class="card-box-b"> <li class="card10"> <a href="/app/99454.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241019/671393f16002e.jpg" alt="驯龙高手旅程安卓版" /> </div> <div class="figure-cont"> <p class="figure-head">驯龙高手旅程安卓版</p> <div class="figure-desc"> <span>模拟经营</span> <span>驯龙高手旅程安卓版</span> </div> <div class="figure-desc"> <p>一款以奇幻世界为背景的模拟经营游戏</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/99397.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241015/670dc626d93dd.png" alt="全民农场最新版" /> </div> <div class="figure-cont"> <p class="figure-head">全民农场最新版</p> <div class="figure-desc"> <span>模拟经营</span> <span>全民农场最新版</span> </div> <div class="figure-desc"> <p>一款轻松风格的农场经营类型的游戏</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/99010.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc0ba5ab7021.jpg" alt="散修功德模拟器完整版" /> </div> <div class="figure-cont"> <p class="figure-head">散修功德模拟器完整版</p> <div class="figure-desc"> <span>模拟经营</span> <span>散修功德模拟器完整版</span> </div> <div class="figure-desc"> <p>散修功德模拟器游戏是一款以功德修炼为题材的模拟游戏</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/99001.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc094ae30941.jpg" alt="我的毕业日记无限金币下载" /> </div> <div class="figure-cont"> <p class="figure-head">我的毕业日记无限金币下载</p> <div class="figure-desc"> <span>模拟经营</span> <span>我的毕业日记无限金币下载</span> </div> <div class="figure-desc"> <p>开始毕业后的新生活吧!</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/98996.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dbd35fadafd1.png" alt="城堡之梦拼图和装饰下载最新版" /> </div> <div class="figure-cont"> <p class="figure-head">城堡之梦拼图和装饰下载最新版</p> <div class="figure-desc"> <span>模拟经营</span> <span>城堡之梦拼图和装饰下载最新版</span> </div> <div class="figure-desc"> <p>城堡之梦拼图和装饰玩家将会在游戏当中感受更多的乐趣体验,你可</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> </ul> </section> <footer class="foot"> <a href="/" class="logo-icon"> <img src="/mobile/images/logo2.png" alt="一聚教程网"> </a> <p>Copyright © 2010-2022</p> <p>111cn.net All Rights Reserved</p> </footer> <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/funcmob.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> <div class="back-top" style="display: block;"> <span class="icon-box"> <svg class="icon" viewBox="0 0 1024 1024"> <path d="M213.333333 640h170.666667v256h256v-256h170.666667l-298.666667-341.333333zM170.666667 128h682.666666v85.333333H170.666667z" fill="#0374f3"></path> </svg> </span> </div> </div> <script src="/js/stat.js"></script> </body> </html>