jquery上傳圖片---本地瀏覽圖片

來源:互聯網
上載者:User

標籤:

//說明:圖片上傳預覽外掛程式//上傳的時候可以產生固定寬高範圍內的等比例縮放圖//參數設定://width                     存放圖片固定大小容器的寬//height                    存放圖片固定大小容器的高//imgDiv                    頁面DIV的JQuery的id//maxSize圖片大小最大限制(K)//imgType                   數組尾碼名//**********************圖片上傳預覽外掛程式*************************(function ($) {    jQuery.fn.extend({        uploadPreview: function (opts) {            opts = jQuery.extend({                width: 0,                height: 0,                imgDiv: null,                imgBox: null,                maxSize: 300,                noimg: ‘‘,                imgType: ["gif", "jpeg", "jpg", "bmp", "png"],                callback: function () { return false; }            }, opts || {});            var _this = $(this);            var imgDiv = opts.imgDiv;            var imgbox = opts.imgBox;            //var image = new Image();            //image.onload = function () {            //    var thisw = this.width, thish = this.height, left, top;            //    var rate = (opts.width / thisw < opts.height / thish) ? opts.width / thisw : opts.height / thish;            //    if (rate <= 1) {            //        imgDiv.width(thisw * rate);            //        imgDiv.height(thish * rate);            //    } else {            //        imgDiv.width(thisw);            //        imgDiv.height(thish);            //        rate = 1;            //    }            //    left = (opts.width - thisw * rate) * 0.5;            //    top = (opts.height - thish * rate) * 0.5;            //    imgDiv.css({ "margin-left": left, "margin-top": top });            //    setTimeout(function () {            //        imgDiv.show();            //    }, 200);            //    image = null;            //}            //image.onerror = function () {            //    this.src = noimg;            //    imgDiv.attr(‘src‘, noimg);            //    this.onerror = null;            //}            //image.src = imgDiv.attr(‘src‘);            var version = 7;            var filters = imgbox[0].filters ? true : false;//ie10核心filters是捨棄了的            var v = { //判斷是否是ie                getie: function () {                    if (window.attachEvent) {                        return true;                    }                    else {                        return false;                    }                },                //是否是ie678                getoldie: function () {                    if (this.getie()) {                        if (window.addEventListener) {                            return false;                        }                        else {                            return true;                        }                    }                    else {                        return false;                    }                },                ie9: function () {                    if (navigator.userAgent.indexOf(‘MSIE 9.0‘) > 0) {                        return true;                    }                }            }            version = (v.getoldie || v.ie9) ? 9 : 10;            var fit = (version <= 9 && filters) || (version >= 10 && filters);            var autoScaling = function () {                if (fit) imgbox.get(0).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "image";                var box_width = imgbox.width();                var box_height = imgbox.height();                var img_width = imgDiv.width();                var img_height = imgDiv.height();                if (fit) {                    img_width = box_width;                    img_height = box_height;                }                if (img_width > 0 && img_height > 0) {                    var rate = (opts.width / img_width < opts.height / img_height) ? opts.width / img_width : opts.height / img_height;                    if (rate <= 1) {                        img_width = img_width * rate;                        img_height = img_height * rate;                        imgbox.width(img_width);                        imgbox.height(img_height);                        if (fit) {                            imgbox.get(0).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "scale";                        }                        else {                            imgDiv.width(img_width);                            imgDiv.height(img_height);                        }                    } else {                        imgDiv.width(img_width);                        imgDiv.height(img_height);                    }                    var left = (opts.width - imgbox.width()) * 0.5;                    var top = (opts.height - imgbox.height()) * 0.5;                    imgbox.css({ "margin-left": left, "margin-top": top });                    setTimeout(function () {                        imgDiv.show();                        if (fit) {                            imgbox.show();                        }                    }, 100);                }                else {                    imgbox.get(0).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "scale";                    imgbox.width(opts.width);                    imgbox.height(opts.height);                }            };            var createImg = function () {                imgDiv.html(‘‘);                var img = $("<img />");                imgDiv.replaceWith(img);                imgDiv = img;            };            _this.change(function () {                imgDiv.hide();                imgDiv.css({ ‘width‘: ‘‘, ‘height‘: ‘‘ });                imgbox.css({ ‘width‘: ‘‘, ‘height‘: ‘‘, ‘padding‘: ‘‘ });                if (this.value) {                    if (!RegExp("\.(" + opts.imgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {                        alert("圖片類型必須是" + opts.imgType.join(",") + "中的一種");                        this.value = "";                        return false;                    }                    //ie7\8\9                    var i = this;                    if (fit) {                        try {                            this.select();                            var imgSrc = document.selection.createRange().text;                            imgbox.html(‘‘);                            imgbox.css({ filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)" });                            imgbox[0].filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "scale";                            imgbox[0].filters.item(‘DXImageTransform.Microsoft.AlphaImageLoader‘).src = imgSrc;                            imgbox.hide();                            setTimeout(function () {                                autoScaling();                            }, 100);                        } catch (e) {                            alert("無效的圖片檔案!");                            return;                        }                    }                    else {                        try {                            var file = null;                            var size = 0;                            if (this.files && this.files[0]) {                                file = this.files[0];                                size = file.size;                            } else if (this.files && this.files.item(0)) {                                file = this.files.item(0);                                size = file.fileSize;                            }                            if ((size / 1024) > opts.maxSize) {                                alert(‘圖片大小不能超過‘ + opts.maxSize + ‘K‘);                                return false;                            }                            //支援html5的瀏覽器,比如高版本的firefox、chrome、ie10                            if (this.files && this.files[0]) {                                if ((this.files[0].size / 1024) > opts.maxSize) {                                    alert(‘圖片大小不能超過‘ + opts.maxSize + ‘K‘);                                    return false;                                }                                var reader = new FileReader();                                reader.onload = function (e) {                                    imgDiv.attr(‘src‘, e.target.result);                                };                                reader.readAsDataURL(this.files[0]);                                setTimeout(function () {                                    autoScaling();                                }, 100);                            }                            //Firefox非捆綁軟體 版本都30幾點幾了,還會有7.??                            //createImg();                            //Firefox 因安全性問題已無法直接通過input[file].value 擷取完整的檔案路徑                            //try {                            //    //Firefox7.0 以下                                                //    imgDiv.attr(‘src‘, file.getAsDataURL());                            //} catch (e) {                            //    //Firefox8.0以上                                                //    imgDiv.attr(‘src‘, window.URL.createObjectURL(file));                            //}                            //setTimeout(function () {                            //    autoScaling();                            //}, 100);                        } catch (e) {                            alert(‘系統錯誤,請重試!‘);                        };                    }                }            });        }    });})(jQuery);

  調用:

<div class="file-box">   <p>圖片上傳</p>   <div class="file-info">     <div class="show-img" id="imgbox2">       <img src="/images/ico-upfileimg.png" id="img2" />     </div>   </div>   <div class="file-input">點擊上專<input type="file" id="file2" name="CFileUp" /></div></div>

  說明:由於ie早期是用濾鏡實現所需要一個div層如上面的

 <div class="show-img" id="imgbox2">
   $(function () {        $("#file1").uploadPreview({            width: 220,            height: 150,            imgDiv: $(‘#img1‘),            imgBox: $(‘#imgbox1‘),            noimg: ‘/Images/noneimg.jpg‘,            callback: function () { }        });          });

  

jquery上傳圖片---本地瀏覽圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.