Created
October 9, 2024 17:19
-
-
Save isocroft/9a12605b23713af4dd3eef4e6e9de565 to your computer and use it in GitHub Desktop.
How to upload 50 GB of file data using client-side JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fileInput = window.document.getElementById('file_input'); | |
| const fileUploadChunkSize = 10 * Math.pow(1024, 2) // 10 by 1MB = 10MB | |
| fileInput.adddEvenListener('change', (event) => { | |
| const [ file ] = event.target.files; | |
| const reader = file.stream().getReader(); | |
| let uploadedBytes = 0; | |
| console.info('Upload Start >'); | |
| reader.read().then(function process ({ done, value }) { | |
| if (done) { | |
| return console.info('Upload Complete!'); | |
| } | |
| window.fetch("/upload", { | |
| method: "POST", | |
| headers: { | |
| 'Content-Range': `bytes ${uploadedBytes}-${uploadedBytes + value.length}/${file.size}` | |
| }, | |
| body: value | |
| }).then(() => { | |
| uploadedBytes += value.length; | |
| console.info(`Upload Progress: ${(uploadedBytes / file.size * 100).toFixed(2)}%`); | |
| return reader.read().then(process); | |
| }).catch(() => console.info('Retrying...') && reader.read().then(process)); | |
| }); | |
| }); |
isocroft
commented
Oct 22, 2025
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment