jquery 上传图片即时预览功能

时间:2023-03-08 16:36:23

<script type="text/javascript">
        jQuery.fn.extend({
            uploadPreview: function (opts) {
                var _self = this, _this = $(this);
                opts = jQuery.extend({
                    Img: "Image1",
                    Width: 100,
                    Height: 100,
                    ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
                    Callback: function () { }
                }, opts || {});
                _self.getObjectURL = function (file) {
                    var url = null;
                    if (window.createObjectURL != undefined) {
                        url = window.createObjectURL(file);
                    } else if (window.URL != undefined) {
                        url = window.URL.createObjectURL(file);
                    } else if (window.webkitURL != undefined) {
                        url = window.webkitURL.createObjectURL(file);
                    }
                    return url;
                }
                _this.change(function () {
                    if (this.value) {
                        if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
                            alert("选择文件错误,图片类型必须是" + opts.ImgType.join(",") + "中的一种");
                            this.value = "";
                            return false;
                        }
                        if (navigator.userAgent.indexOf("MSIE") > -1) {
                            try {
                                $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]));
                            } catch (e) {
                                var src = "";
                                var obj = $("#" + opts.Img);
                                var div = obj.parent("div")[0];
                                _self.select();
                                if (top != self) {
                                    window.parent.document.body.focus();
                                } else {
                                    _self.blur();
                                }
                                src = document.selection.createRange().text;
                                document.selection.empty();
                                obj.hide();
                                obj.parent("div").css({
                                    'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
                                    'width': opts.Width + 'px',
                                    'height': opts.Height + 'px'
                                });
                                div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src;
                            }
                        } else {
                            $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]));
                        }
                        opts.Callback();
                    }
                });
            }
        });
    </script>
    <script type="text/javascript">
        $(function () {
//            $("#up").uploadPreview({ Img: "ImgPr", Width: 120, Height: 120 });
            $("#<%=FileUpload1.ClientID%>").uploadPreview({ Img: "Img1", Width: 120, Height: 120 });
            $("#<%=photopath.ClientID%>").uploadPreview({ Img: "Image1", Width: 120, Height: 120 });
        });
    </script>

--以下是获取客户端上传文件的名称

<script type="text/javascript">         function checkType() {             //得到上传文件的值

var fileName1 = document.getElementById("FileUpload1").value;             var fileName2 = document.getElementById("FileUpload2").value;             if (fileName1 = document.getElementById("FileUpload1").value) {

//返回String对象中子字符串最后出现的位置.                   var seat = fileName1.lastIndexOf(".");

//返回位于String对象中指定位置的子字符串并转换为小写.                   var extension = fileName1.substring(seat).toLowerCase();

//判断允许上传的文件格式                   //if(extension!=".jpg"&&extension!=".jpeg"&&extension!=".gif"&&extension!=".png"&&extension!=".bmp"){                   //alert("不支持"+extension+"文件的上传!");                   //return false;                   //}else{                   //return true;                   //}   //                //                var allowed = [".jpg", ".gif", ".png", ".bmp", ".jpeg", ".swf", ".flv", ".wmv", ".mp3", ".mp4", ".avi", ".rmvb",".rar",".png",".tga"]; //                for (var i = 0; i < allowed.length; i++) { //                    if (!(allowed[i] != extension)) { //                        return true; //                    } //                } //                alert("不支持" + extension + "格式"); //                return false;             }             else if(fileName2 = document.getElementById("FileUpload2").value)             {                 var seat1 = fileName2.lastIndexOf(".");                 //返回位于String对象中指定位置的子字符串并转换为小写.                   var extension = fileName2.substring(seat1).toLowerCase();

//判断允许上传的文件格式                   //if(extension!=".jpg"&&extension!=".jpeg"&&extension!=".gif"&&extension!=".png"&&extension!=".bmp"){                   //alert("不支持"+extension+"文件的上传!");                   //return false;                   //}else{                   //return true;                   //}

var allowed = [".jpg", ".gif", ".png", ".bmp", ".jpeg", ".swf", ".flv", ".wmv", ".mp3", ".mp4", ".avi", ".rmvb", ".rar", ".png", ".tga"];                 for (var i = 0; i < allowed.length; i++) {                     if (!(allowed[i] != extension)) {                         return true;                     }                 }                 alert("不支持" + extension + "格式");                 return false;             }

}      </script>