js chunk uploader
2020-04-08 -> 2020-05-14
A js tool to help implementing a chunk uploading system.
This can be used as the js client in a simple-chunk-upload-protocol communication.
Install
npm i chunk-uploader
Usage
Example 1: the basics
const ChunkUploader = ; // assuming file is a js File object{ let data = firstName: "paul" csrf_token: "abc" whatever: "dude" ; let chunkUploader = serverUrl: "/my-upload-server.php" chunkSize: 2 * 1024 * 1024 // 2M { // do something... } { let percent = Math + "%"; // update your gui... } ; let response = await chunkUploader; let jsonResponse = await response; // for instance}
Example 2: handling failure at the chunk level
In this example we use the onChunkResponseReceived callback to test every chunk's response. If a chunk's response fails, the upload of the file stops immediately.
This allows us to fail early, rather than having to wait until the end of the upload before triggering an error message.
const ChunkUploader = ; { let chunkUploader = serverUrl: optionsserverUrl // chunkSize: 5 * 1024 * 1024, { console; } { let percent = Math + "%"; // update your gui... } { try let jsonResponse = await response; // analyze the chunk response here, and return true if ok... return true; catch e // otherwise return a message error, this will interrupt the upload. // or return false to use a default error message return e; } ; let file = 0 // replace with some js File object let data = {}; // extra data if necessary, this is sent via post let response = await chunkUploader; let jsonResponse = await response; } ;
History log
-
1.2.0 -- 2020-05-14
- add onChunkResponseReceived callback option
-
1.1.0 -- 2020-04-20
- Update sendByChunks, now handles data parameter recursively
-
1.0.1 -- 2020-04-13
- add link in README.md
-
1.0.0 -- 2020-04-08
- initial commit