图片上传预览,兼容IE6、IE7、IE8和firefox浏览器

传Firefox浏览器对于上传文件的安全问题,只能得到绝对路径。如果像上传图片预览,可实现如下:

注意,此处实现的思路是,IE浏览器把预览图片显示在div中,而firefox浏览器只能显示在img标签中。

JS代码:
function changePhoto(sender){
  if(userAgent.indexOf("firefox")>=1){//fiefox浏览器
      alert(sender.files[0]);
      document.getElementByIdx('image0').src=sender.files[0].getAsDataURL();
    }elseif(window.ActiveXObject){//IE浏览器

       var newPreview = document.getElementByIdx("newPreview");
       newPreview.innerHTML="";
       newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader")
       .src=document.getElementByIdx('myFile').value;
       newPreview.style.width= "120px";
      newPreview.style.height = "154px";
      newPreview.style.border="1px solid #a3b7cf";

    }

}



页面如下:

<style type="text/css">
#newPreview
{
   filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
}
</style>

 

<div class="cl_left1" id="info2"name="info2">
    <a><span>照片上传:</span><inputtype="text" class="text1"  id="filename"name="filename" />
   <spanclass="file"></span>
   <input class="file_0" type="file" name="myFile"id="myFile" οnchange="changePhoto(this)"/>
     </a>
  </div>

 

<div id="newPreview">
   <aclass="img_head" id="img_head"><imgid="image0"src="<%=path%>/<%=photo%>"<%=path%>/A3IAIo/person1.jpg'"
 height="154px;"width="120px;"/>

</a> 
  </div>


 

让Firefox显示本地文件的唯一办法就是修改它的默认安全策略:

  1. 在Firefox的地址栏中输入“about:config
  2. 继续输入“security.checkloaduri
  3. 双击下面列出来的一行文字,把它的值由true改为false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值