Method of front-end accept file Call background upload file

Source: Internet
Author: User

Need to use

Slice () method

Reference

Http://www.w3school.com.cn/jsref/jsref_slice_array.asp

The front-end HTML usage tag is

Type=file input tag, you can directly select File upload

Js:

 function () {        varFile = $ ("#SelectImageFile") [0].files[0]; varFileName =File.name; varFilesplitsize = 1 * 1024 * 1024;//file Fragment size 5M        varSize =file.size; varPostURL = Postweburl;//Background method address for uploading files        varFormData =NewFormData (); varIndex1 = Filename.lastindexof (".")); varIndex2 =filename.length; varsuffix = filename.substring (index1, INDEX2);//suffix name        //If you need to limit the suffix name, you can write this        if(Suffix! = ". rar" && suffix! = ". zip" && suffix! = ". 7z") {msg ("The uploaded image information must be. rar,.zip,.7z format"); } Else{formdata.append ("File", File.slice (start, start +filesplitsize)); Formdata.append ("Name", FileName); $.ajax ({url:posturl, type:' POST ', Data:formdata,//tell jquery not to process the data sentProcessData:false,//tell jquery not to set the Content-type request headerContentType:false, Beforesend:function () {                    //Console.log ("In progress, please wait");                },                //multipart upload, 1-5 upload successful, then upload 6-11, and so on, after successful, give a hint, before success, will always show the upload progressSuccessfunction(data) {$ ("#process"). Show ();//Progress bar Display                    if(Start + filesplitsize >=size) {                        $("#spnProcessShow"). CSS ("width", "100%"); $("#spnProcessText"). HTML ("Upload completed, uploaded 100%"); } Else{Start+=filesplitsize; Percentage= Math.Round (start/size * 100); $("#spnProcessShow"). CSS ("width", Percentage + "%")); $("#spnProcessText"). HTML ("uploading, uploaded" + Percentage + "%")); }}, Error:function(data) {Start= 0; $("#process"). Hide ();//Hide Progress bar$ ("#spnProcessShow"). CSS ("width", "0%"); $("#lblMsg"). HTML ("image upload failed");        }            }); }    }    

Method of front-end accept file Call background upload file

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.