JS--获取file文件全路径(ie和ff已经兼容)

<html>
            <head>
            <title>File Input</title>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <style>
            body{font-size:13px;}
            .box{border:1px solid #eee;background:#ffe;padding:10px 30px;margin:10px;}
            dt{font-weight:bold;}
            dt,img,textarea{margin:8px 0;}
            em{color:#f00; font-style:normal;}
            span{color:#999;}
            .ft{color:#999;font-size:11px;text-align:right}
            </style>
            </head>
            <body>
            <h2>在不同浏览器中获取File Input的value</h2>
            <div class="box">
            <input type="file" name="foo" id="foo" size="60" />
            <input type="button" value="Show Value" οnclick="alert(document.getElementById('foo').value)" />
            <input type="button" value="Show Value in FF3" οnclick="alert(getValueFF('foo'))" />
            <input type="button" value="Show Value in IE8" οnclick="alert(getValueIE8('foo'))" />
            </div>
            <div class="box">
            <dl>
            <dt>Firefox 2.x、IE6、IE7</dt>
            <dd>在本地及远端都可以取得完整地址(包括路径及文件名)</dd>
            </dl>
            <dl>
            <dt>Firefox 3.0</dt>
            <dd>在本地及远端都<em>不能</em>取得完整地址,只能获得文件名</dd>
            </dl>
            <dl>
            <dt>IE8、IE7[IE8模拟]</dt>
            <dd>在本地可以取得完整地址(包括路径及文件名),在远端<em>不能</em>取得完整地址,只能获得文件名</dd>
            </dl>
            <span>* 本地:localhost</span>
            </div>
            <div class="box">
            <h3>如何在Firefox3下取得完整路径</h3>
            <p>
            第一步:打开"about:config"页面,查找"signed.applets.codebase_principal_support"属性,将其值设置为true。
            </p>
            <p>
            第二步:在javascript中采用以下代码进行获取:<br />
            <textarea style="width:800px;height:300px;">
            function getValueFF(id){
            var ip = document.getElementById(id);
            if (ip.files) {
            //ffx3 - try to have access to full path
            try {
            netscape.security.PrivilegeManager.enablePrivilege( 'UniversalFileRead' )
            }
            catch (err) {
            //need to set signed.applets.codebase_principal_support to true
            }
            };
            return ip.value;
            }
            </textarea>
            </p>
            <p>
            经过以上步骤,在Firefox3下获取file input的value时,会弹出一个Internet Security的对话框,点击"Allow"将可以获取完整value。<br />
            <span>资料:<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=143220" target="_blank">https://bugzilla.mozilla.org/show_bug.cgi?id=143220</a></span>
            </p>
            </div>
            <div class="box">
            <h3>如何在IE8下取得完整路径</h3>
            <p>方法一:使用selection.createRange <br />
            <textarea style="width:800px;height:150px;">
            function getValueIE8(id){
            var ip = document.getElementById(id);
            ip.select();
            return document.selection.createRange().text;
            }
            </textarea>
            </p>
            <p>方法二:引用:<a href="https://bugzilla.mozilla.org/attachment.cgi?id=328849" target="_blank">https://bugzilla.mozilla.org/attachment.cgi?id=328849</a></p>
            </div>
            <script language="JavaScript">
            function getValueFF(id){
            var ip = document.getElementById(id);
            if (ip.files) {
            //ffx3 - try to have access to full path
            try {
            netscape.security.PrivilegeManager.enablePrivilege( 'UniversalFileRead' )
            }
            catch (err) {
            //need to set signed.applets.codebase_principal_support to true
            }
            };
            return ip.value;
            }
            function getValueIE8(id){
            var ip = document.getElementById(id);
            ip.select();
            return document.selection.createRange().text;
            }
            </script>
            </body>
            </html>

 开发者博客www.developsearch.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值