slice large file into chunks and upload using ajax and html5 FileReader

前端 未结 3 1910
借酒劲吻你
借酒劲吻你 2020-12-14 21:52

What I want to implement is:

In the front end, I use the html5 file api to read the file, and then upload the file\'s content to the php backend using ajax, and it\'

3条回答
  •  忘掉有多难
    2020-12-14 22:35

    • Using the readAsBinaryString fn is just bad practice
    • SendAsBinary is also depricated
    • Reading the chunks content is just pure dum. Slicing them is enough. xhr.send(blob.slice(0,10))
    • Slicing is also unnecessary unless the server don't accept such large files (such as dropbox limited REST API)
    • So if anyone trying to be smart about using worker threads, base64 or FileReader for uploading large files - don't do that, it's all unnecessary.

    Only time it's okey to read/slice the file is if you are deciding to encrypt/decrypt/zip the files before sending it to the server.
    But only for a limited time until all browser start supporting streams.
    Then you should take a look at fetch and ReadableStream

    fetch(url, {method: 'post', body: new ReadableStream({...})})
    

    if you just need to forward the blob to the server, just do: xhr.send(blob_or_file) and the browser will take care of reading it (correctly) and not consume any memory. And the file can be however large the file/blob is

提交回复
热议问题