JS implementation of large file multipart upload method

Source: Internet
Author: User

With the help of JS Blob object Formdata object can realize large file multipart upload function, the specific use of BLOB and formdata can go to the following address to see
Use of FormData objects
Use of Blob objects
The following is the implementation code, in this case, the back-end code is implemented using PHP, just to demonstrate the basic functionality, specific file validation logic is ignored.
Previous section code:

<!    DOCTYPE html>varBytesperpiece = 1024 * 1024;//Each file slice size is set to 1MB.        vartotalpieces; //Send Request        functionupload () {varBlob = document.getElementById ("file"). files[0]; varStart = 0; varend; varindex = 0; varFileSize =blob.size; varfilename =Blob.name; //calculate the total number of file slicesTotalpieces = Math.ceil (FileSize/bytesperpiece);  while(Start <filesize) {End= Start +bytesperpiece; if(End >filesize) {End=filesize; }                varChunk = Blob.slice (start,end);//Cutting Files                varsliceindex= Blob.name +index; varFormData =NewFormData (); Formdata.append ("File", chunk, filename); $.ajax ({URL:' Http://localhost:9999/test.php ', type:' POST ', Cache:false, Data:formdata, ProcessData:false, ContentType:false,                }). Done (function(res) {}). Fail (function(res) {}); Start=end; Index++; }        }    </script></body>

Back-end PHP code:

<? PHP Header (' access-control-allow-origin:* '); Header ("Access-control-allow-headers:origin, X-requested-with, Content-type, Accept"); $file $_files [' file ']; $filename $file [' name ']; file_put_contents ($filenamefile_get_contents($file[' tmp_name ']), file_append);

JS implementation of large file multipart upload method

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.