jquery實現圖片上傳之前預覽的方法_jquery

來源:互聯網
上載者:User

本文執行個體講述了jquery實現圖片上傳之前預覽的方法。分享給大家供大家參考。具體實現方法如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><script src="jquery-1.8.1.min.js" type="text/javascript"></script><script>/**參數說明: Img:圖片ID;Width:預覽寬度;Height:預覽高度;ImgType:支援檔案類型;Callback:選擇檔案顯示圖片後回調方法;*使用方法: <div><img id="ImgPr" width="120" height="120" /></div><input type="file" id="up" />把需要進行預覽的IMG標籤外 套一個DIV 然後給上傳控制項ID給予uploadPreview事件$("#up").uploadPreview({ Img: "ImgPr", Width: 120, Height: 120, ImgType: ["gif", "jpeg", "jpg", "bmp", "png"], Callback: function () { }});*/jQuery.fn.extend({  uploadPreview: function (opts) {    var _self = this,      _this = $(this);    opts = jQuery.extend({      Img: "ImgPr",      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 ($.browser.msie) {          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()      }    })  }});$(function () {$("#up").uploadPreview({ Img: "ImgPr", Width: 120, Height: 120 });});</script></head><body><div style="width:500px;margin:0px auto;"><h2>圖片上傳預覽示範</h2><div><img id="ImgPr" width="120" height="120" /></div><input type="file" id="up" /></div></body></html>

希望本文所述對大家的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.