使用Javascript预览本地图片

本文介绍了一种兼容IE和Firefox等浏览器的本地文件图片预览解决方案,利用IE的DXImageTransform滤镜和Firefox的getAsDataURL方法实现跨浏览器的图片预览功能。

出于安全的考虑,现在的浏览器都不允许...src = file:///...这样的写法,作为替代,IE可以使用“DXImageTransform.Microsoft.AlphaImageLoader”滤镜的方式,而Firefox则提供了一个getAsDataURL方法。

<html>
02 <head>
03 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
04 <title>preview</title>
05 <style>
06 #preview {
07     width: 270px;
08     height: 129px;
09 }
10 </style>
11 <!--[if IE]>
12 <style>
13 #preview {
14     filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);
15 }
16 </style>
17 <![endif]-->
18 </head>
19
20 <body>
21 <input id="file" type="file" />
22 <div id="preview">
23     <img id="image" src="http://www.baidu.com/img/baidu_logo.gif" />
24 </div>
25 <script>
26 document.getElementById("file").onchange = function() {
27     if ("Microsoft Internet Explorer" == navigator.appName) {
28         var preview = document.getElementById("preview");
29         preview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = this.value;
30         document.getElementById("image").style.display = "none";
31     } else if ("Netscape" == navigator.appName){
32         document.getElementById("image").src = this.files[0].getAsDataURL();
33     } else {
34         alert("Not Support!");
35     }
36 }
37 </script>
38 </body>
39 </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值