Real-time upload of picture previews with JQUERY,HTML5

Source: Internet
Author: User

 html code  <div class  = "Form-group" > <label for  = "pic" class  = "Col-md-1 C Ontrol-label "> Small figure:</label> <div class  =" Col-md-4 "> &L T;input type= "file" class  = "Form-control" name= "pic"/> <div class  = "Progress" ><span class  = "Bgpro" > </span></div> class  = "Pic_show"/> </div> </div> 
CSS code . Progress {    width: 100%;    Height:10px;    Border#CCC;    margin:3px 0;}. Bgpro {    display:block;    Width: 1%;    Background:Green;    Height:10px;    Color:#FFF;}
JS Code<script type= "Text/javascript" >    $(' Input[name= ' pic "] '). On (' Change ',function() {        if(typeof this.files = = ' undefined ')){            return; }        varimg = this.files[0];//Get picture information        varType = Img.type;//get the type of picture and judge the use        varurl = Getobjecturl (this.files[0]);//get the picture local URL using a custom function        varFD =NewFormData ();//instantiate the form and submit the data for useFd.append (' img ', IMG);//Append the img in        if(URL) $ ('. Pic_show '). attr (' src ', url). Show ();//Show Pictures        if(Type.substr(0,5)! = ' image ') {//Invalid type filteringAlert (' Non-picture type, cannot be uploaded! ‘); return; }        //start Ajax request, background with TP$.Ajax ({type: ' Post ',URL: ' __url__/upload_img ',Data: FD,ProcessData:false,//Tell JQuery isn't to process the data, this is a must, otherwise it will be an errorContentType:false,//Tell JQuery don't to set ContentTypeDataType: ' Text ',XHR:function() {//This is the focus, get to the original XHR object, and then bind Upload.onprogress                varXHR = jquery.ajaxsettings.xhr (); XHR. upload.onprogress =function(EV) {//here, start calculating percentages .                    varParcent = 0; if(EV.lengthcomputable) {percent= * Ev.loaded/ev.Total ; Percent= parsefloat (percent). toFixed (2); $('. Bgpro '). CSS (' width ', percent + '% '). HTML (percent + '% '));                }                }; returnXHR; },        });    }); //Custom Get picture path function    functionGetobjecturl (file) {        varURL =NULL ; if(window.createobjecturl!=undefined) {//Basicurl = Window.createobjecturl (file) ; } Else if(Window. url!=undefined) {//Mozilla (Firefox)url = window. Url.createobjecturl (file) ; } Else if(window.webkiturl!=undefined) {//WebKit or Chromeurl = Window.webkitURL.createObjectURL (file) ; }        returnURL; }</script>

Can further beautify, for example, wearing the wrong, you can do an AJAX delete, re-transmission, compared to very easy

The data obtained:

Reference URL: http://blog.csdn.net/a519640026/article/details/18090695

Real-time upload of picture previews with JQUERY,HTML5

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.