Js 本地图片预览功能的代码举例

发布时间:2020-02-08编辑:脚本学堂
本文介绍一个用Javascript实现的本地图片预览的代码,可以兼容ie[6-8]、火狐、Chrome17+、Opera11+、Maxthon3等浏览器,有需要的朋友,可以参考下。

代码如下:
 

复制代码 代码示例:
<script language="javascript">
/**
 *js本地图片预览,兼容ie[6-8]、火狐、Chrome17+、Opera11+、Maxthon3
 * www.jb200.com at 2013/5/8 星期三
 */
    function PreviewImage(obj, imgPreviewId, divPreviewId) {
        var allowExtention = ".jpg,.bmp,.gif,.png"; //,允许上传文件的后缀名
        var extention = obj.value.substring(obj.value.lastIndexOf(".") + 1).toLowerCase();
        var browserVersion = window.navigator.userAgent.toUpperCase();
        if (allowExtention.indexOf(extention) > -1) {
            if (browserVersion.indexOf("MSIE") > -1) {
                if (browserVersion.indexOf("MSIE 6.0") > -1) {//ie6
                    document.getElementByIdx_x_x_x_x(imgPreviewId).setAttribute("src", obj.value);
                } else {//ie[7-8]、ie9未测试
                    obj.select();
                    var newPreview = document.getElementByIdx_x_x_x_x(divPreviewId + "New");
                    if (newPreview == null) {
                        newPreview = document_createElement_x_x_x_x("div");
                        newPreview.setAttribute("id", divPreviewId + "New");
                        newPreview.style.width = 160;
                        newPreview.style.height = 170;
                        newPreview.style.border = "solid 1px #d2e2e2";
                    }
                    newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + document.selection.createRange().text + "')";
                    var tempDivPreview = document.getElementByIdx_x_x_x_x(divPreviewId);
                    tempDivPreview.parentNode.insertBefore(newPreview, tempDivPreview);
                    tempDivPreview.style.display = "none";
                }
            } else if (browserVersion.indexOf("FIREFOX") > -1) {//firefox
                var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox/([d.]+)/)[1]);
                if (firefoxVersion < 7) {//firefox7以下版本
                    document.getElementByIdx_x_x_x_x(imgPreviewId).setAttribute("src", obj.files[0].getAsDataURL());
                } else {//firefox7.0+                   
                    document.getElementByIdx_x_x_x_x(imgPreviewId).setAttribute("src", window.URL.createObjectURL(obj.files[0]));
                }
            } else if (obj.files) {
                //兼容chrome、火狐等,HTML5获取路径                  
                if (typeof FileReader !== "undefined") {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        document.getElementByIdx_x_x_x_x(imgPreviewId).setAttribute("src", e.target.result);
                    }
                    reader.readAsDataURL(obj.files[0]);
                } else if (browserVersion.indexOf("SAFARI") > -1) {
                    alert("暂时不支持Safari浏览器!");
                }
            } else {
                document.getElementByIdx_x_x_x_x(divPreviewId).setAttribute("src", obj.value);
            }
        } else {
            alert("仅支持" + allowSuffix + "为后缀名的文件!");
            obj.value = ""; //清空选中文件
            if (browserVersion.indexOf("MSIE") > -1) {
                obj.select();
                document.selection.clear();
            }
            obj.outerHTML = obj.outerHTML;
        }
    }
</script>

调用:
 

<asp:FileUpload ID="ImgUploads" runat="server" onchange="PreviewImage(this,'imgView','divNewPreview');" />
<img id="imgView" src="默认图片路径"  alt="" />