Jquery allows you to preview images uploaded locally in a browser.

Source: Internet
Author: User
The code of the Image Upload local preview function can be searched on the Internet, but it is more compatible with browsers. This article provides a good example. If you are interested, refer I. Local preview of image uploading

Most of the Image Upload functions need to be previewed locally. In order to better enable users to experience the effect and prove the finished product, you need to be compatible with several browsers, all examples are integrated with this example plug-in, which is compatible with Firefox, Google, and ie8. Others have not been tested.

The Code is as follows:


(Function ($ ){
JQuery. fn. extend ({
UploadPreview: function (opts ){
Opts = jQuery. extend ({
Width: 0,
Height: 0,
ImgPreview: null,
ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
Callback: function () {return false ;}
}, Opts | {});

Var _ self = this;
Var _ this = $ (this );
Var imgPreview =$ (opts. imgPreview );
// Set the style
AutoScaling = function (){
ImgPreview.css ({"margin-left": 0, "margin-top": 0, "width": opts. width, "height": opts. height });
ImgPreview. show ();
}
// File button event
_ This. change (function (){
If (this. value ){
If (! RegExp ("\. ("+ opts. imgType. join ("|") + ") $", "I "). test (this. value. toLowerCase ())){
Alert ("the image type must be" + opts. imgType. join (",") + ");
This. value = "";
Return false;
}
If ($. browser. msie) {// judge ie
Var path = $ (this). val ();
If (/"\ w \ W"/. test (path )){
Path = path. slice (1,-1 );
}
ImgPreview. attr ("src", path );
ImgPreview.css ({"margin-left": 0, "margin-top": 0, "width": opts. width, "height": opts. height });
SetTimeout ("autoScaling ()", 100 );
}
Else {
If ($. browser. version <7 ){
ImgPreview. attr ('src', this. files. item (0). getAsDataURL ());
}
Else {
OFReader = new FileReader (), rFilter =/^ (? : Image \/bmp | image \/cis \-cod | image \/gif | image \/ief | image \/jpeg | image \ /pipeg | image \/png | image \/svg \ + xml | image \/tiff | image \/x \-cmu \-raster | image \/x \-cmx | image \/x \-icon | image \/x \-portable \-anymap | image \/x \-portable \-bitmap | image \/x \-portable \-graymap | image \/x \-portable \-pixmap | image \/x \-rgb | image \/x \-xbitmap | image \/x \-xpixmap | image \/x \-xwindowdump) $/I;
OFReader. onload = function (oFREvent ){
ImgPreview. attr ('src', oFREvent.tar get. result );
};
Var oFile = this. files [0];
OFReader. readAsDataURL (oFile );
}
ImgPreview.css ({"margin-left": 0, "margin-top": 0, "width": opts. width, "height": opts. height });
SetTimeout ("autoScaling ()", 100 );
}
}
Opts. callback ();
});
}
});
}) (JQuery );


Ii. Call Method

The Code is as follows:


JQuery (function (){
JQuery ("# idFile1"). uploadPreview ({
Width: 100,
Height: 100,
ImgPreview: "# idImg1 ",
ImgType: ["bmp", "gif", "png", "jpg"],
Callback: function (){
Ip1 ();
Return false;
}
});
);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.