Upload Image preview, support IE6

Source: Internet
Author: User

//Description: Image upload Preview plugin//when uploading, you can generate equal scale graphs in a fixed width and height range.//parameter settings://Width store picture fixed size container width//height storage picture fixed size container high//Imgdiv The ID of the jquery page Div//maxSize maximum picture size limit (K)//imgtype array suffix name//********************** image upload Preview plugin *************************(function($) {jQuery.fn.extend ({uploadpreview:function(opts) {opts=jquery.extend ({width:0, Height:0, Imgdiv:"#imgDiv", MaxSize:300, Imgtype: ["GIF", "JPEG", "JPG", "BMP", "PNG"], callback:function() {return false; } }, OPTs|| {}); //var _self = this;            var_this = $ ( This); varImgdiv =$ (OPTS.IMGDIV);            Imgdiv.width (Opts.width);            Imgdiv.height (Opts.height); varVersion = parseint ($.browser.version, 10); AutoScaling=function () {                if(Version = = 7 | | version = 8 | | version = = 9) imgdiv.get (0). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). Sizingmethod = "image"; varImg_width =imgdiv.width (); varImg_height =imgdiv.height (); if(Img_width > 0 && img_height > 0) {                    varRate = (Opts.width/img_width < opts.height/img_height)? Opts.width/img_width:opts.height/Img_height; if(Rate <= 1) {                        if(Version = = 7 | | version = 8 | | version = = 9) imgdiv.get (0). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). Sizingmethod = "scale"; Imgdiv.width (Img_width*Rate ); Imgdiv.height (Img_height*Rate ); } Else{imgdiv.width (img_width);                    Imgdiv.height (Img_height); }                    varleft = (Opts.width-imgdiv.width ()) * 0.5; vartop = (Opts.height-imgdiv.height ()) * 0.5; Imgdiv.css ({"Margin-left": Left, "Margin-top": Top});                Imgdiv.show (); }}, Createimg=function() {imgdiv.html (‘‘); varIMG = $ (");                Imgdiv.replacewith (IMG); Imgdiv=img; }, _this.change (function () {                if( This. Value) {                    if(! REGEXP ("\. (" + Opts.imgType.join ("|") + ") $", "I"). Test ( This. Value.tolowercase ())) {Alert ("Picture type must be" + opts.imgType.join (",") + ");  This. Value = ""; return false;                    } imgdiv.hide (); if($.browser.msie && Version < 10) {                        if(Version = = 6) {                            varImage =NewImage (); Image.onload=function () {                                if((image.filesize/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; }} image.src= ' file:///' + This. Value;                            Createimg (); Imgdiv.attr (' SRC ', IMAGE.SRC);                        AutoScaling (); } Else {                             This. Select (); varIMG =Document.selection.createRange (). text; varImage =NewImage (); Image.onload=function () {                                if((image.filesize/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; }} image.src=img; Imgdiv.html (‘‘); Imgdiv.css ({filter:"Progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=image)" }); Imgdiv.get (0). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). Sizingmethod = "image"; Try{imgdiv.get (0). Filters.item (' DXImageTransform.Microsoft.AlphaImageLoader '). src =img; } Catch(e) {alert ("Invalid picture File! "); return; } setTimeout ("AutoScaling ()", 100); }                    }                    Else {                        Try {                            varFile =NULL; varSize = 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 (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false;                            } createimg (); //Firefox is unable to get the full file path directly through Input[file].value due to security issues                            Try {                                //Firefox7.0 belowImgdiv.attr (' src ', File.getasdataurl ()); } Catch(e) {//Firefox8.0 or moreImgdiv.attr (' src ', window.                            Url.createobjecturl (file)); } imgdiv.css ({"Vertical-align": "Middle" }); SetTimeout ("AutoScaling ()", 100); } Catch(e) {//browsers that support HTML5, such as high-version Firefox, Chrome, Ie10                            if( This. Files && This. files[0]) {                                if(( This. files[0].size/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; }                                varReader =NewFileReader (); Reader.onload=function(e) {imgdiv.attr (' SRC ', E.target.result);                                }; Reader.readasdataurl ( This. files[0]); SetTimeout ("AutoScaling ()", 100);                    }                        };        }                }            }); }    });}) (jQuery);

HTML code:

  <Divstyle= "width:290px; height:290px; overflow:hidden; border:1px solid red;">        <DivID= "Imgdiv"></Div>    </Div>    <BR>    <inputtype= "File"value= "Upload file">    <Script>$ (document). Ready (function () {            $("input"). Uploadpreview ({width:290, Height:290, Imgdiv:"#imgDiv" }); });</Script>

Upload Image preview, support IE6

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.