js实现预览图片

原文:http://blog.csdn.net/goodnew/article/details/17762567

<html>                                                                                                                                                                                            

<head>                                                                                                                                                                                            
<title>js图片上传预览</title>                                                                                                                                                                           
<script>                                                                                                                                                                                          
function PreviewImage(imgFile)                                                                                                                                                                    
{                                                                                                                                                                                                 
    var filextension=imgFile.value.substring(imgFile.value.lastIndexOf("."),imgFile.value.length);                                                                                                
    filextension=filextension.toLowerCase();                                                                                                                                                      
    if ((filextension!='.jpg')&&(filextension!='.gif')&&(filextension!='.jpeg')&&(filextension!='.png')&&(filextension!='.bmp'))                                                                  
    {                                                                                                                                                                                             
        alert("对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !");                                                                                                                                               
        imgFile.focus();                                                                                                                                                                          
    }                                                                                                                                                                                             
    else                                                                                                                                                                                          
    {                                                                                                                                                                                             
        var path;                                                                                                                                                                                 
        if(document.all)//IE                                                                                                                                                                      
        {                                                                                                                                                                                         
            imgFile.select();                                                                                                                                                                     
            path = document.selection.createRange().text;                                                                                                                                         
            document.getElementById("imgPreview").innerHTML="";                                                                                                                                   
            document.getElementById("imgPreview").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")";//使用滤镜效果          
        }                                                                                                                                                                                         
        else//FF                                                                                                                                                                                  
        {                                                                                                                                                                                         
            path=window.URL.createObjectURL(imgFile.files[0]);// FF 7.0以上                                                                                                                         
            //path = imgFile.files[0].getAsDataURL();// FF 3.0                                                                                                                                    
            document.getElementById("imgPreview").innerHTML = "<img id='img1' width='120px' height='100px' src='"+path+"'/>";                                                                     
            //document.getElementById("img1").src = path;                                                                                                                                         
        }                                                                                                                                                                                         
    }                                                                                                                                                                                             
}                                                                                                                                                                                                 
</script>                                                                                                                                                                                         
</head>                                                                                                                                                                                           
<body>                                                                                                                                                                                            
<input type="file" οnchange='PreviewImage(this)' />                                                                                                                                               
<br />                                                                                                                                                                                            
<div id="imgPreview" style='width:120px; height:100px;'>                                                                                                                                          
    <img id="img1" src="" width="120" height="100" />                                                                                                                                             
</div>                                                                                                                                                                                            
</body>                                                                                                                                                                                           
</html>                                                                                                                                                                                           
                                                                                                                                                                                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值