Skip to content

Instantly share code, notes, and snippets.

@dsine-de
Last active May 16, 2024 08:21
Show Gist options
  • Select an option

  • Save dsine-de/f5c316581f169f44a7ce3027cae88bfd to your computer and use it in GitHub Desktop.

Select an option

Save dsine-de/f5c316581f169f44a7ce3027cae88bfd to your computer and use it in GitHub Desktop.
Fetch API Stream Upload
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