Last active
May 16, 2024 08:21
-
-
Save dsine-de/f5c316581f169f44a7ce3027cae88bfd to your computer and use it in GitHub Desktop.
Fetch API Stream Upload
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
| let bytesUploaded = 0; | |
| const transformStream = new TransformStream({ | |
| transform(chunk, controller) { | |
| console.log('progress:', bytesUploaded / blob.size); | |
| bytesUploaded += chunk.byteLength; | |
| controller.enqueue(chunk); | |
| }, | |
| flush() { | |
| console.log('completed'); | |
| } | |
| }); | |
| const response = await fetch('/upload', { | |
| method: 'POST', | |
| body: blob.stream().pipeThrough(transformStream), | |
| duplex: 'half', | |
| headers: {'Content-Type': 'application/octet-stream'} | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment