首页 > Web开发 > 详细

上传图片及时预览

时间:2015-03-24 10:31:36      阅读:191      评论:0      收藏:0      [点我收藏+]

最近写了一个上传图片及时预览的功能,也参考了网上一些人代码,可以支持最新的chrome,FF,Ie,以及Ie6

HTML

1     <img id="imgTag" style="height: 100px;" />
2     <input type="file" onchange="DisplayImage(this,‘imgTag‘)"/>

Js

function DisplayImage(fileTag,imgTagId){
    var allowExtention=".jpg.png.gif";
    var extentionArr=fileTag.value.split(‘.‘);
    var extention = extentionArr[extentionArr.length-1];
    if(!(allowExtention.indexOf(extention)>-1)){
        alert("Please upload image!");
    }else{
        //for adveced broswer(the newest ie,chrome,ff)
        if(typeof(FileReader)!=="undefined"){
            var reader = new FileReader();
            reader.readAsDataURL(fileTag.files[0]);
            reader.onload = function(e){
                document.getElementById(imgTagId).setAttribute("src", e.target.result);
            }
        }else{
        //for(ie6)
        document.getElementById(imgTagId).setAttribute("src",fileTag.value);
        }
    }
}

                                                                   --Ones

上传图片及时预览

原文:http://www.cnblogs.com/ones/p/4361901.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!