Skip to content

Instantly share code, notes, and snippets.

@zdunecki
Created December 10, 2021 12:34
Show Gist options
  • Save zdunecki/0a874687c308eadc22c031636025bde3 to your computer and use it in GitHub Desktop.
Save zdunecki/0a874687c308eadc22c031636025bde3 to your computer and use it in GitHub Desktop.
Send stream from blob URL to API - WIP
const url = "api.example.com/images";
fetch("https://blob:example.com")
.then((response) => response.body)
.then((rb) => {
const reader = rb.getReader();
return new ReadableStream({
start(controller) {
// The following function handles each data chunk
function push() {
// "done" is a Boolean and value a "Uint8Array"
reader.read().then(({ done, value }) => {
// If there is no more data to read
if (done) {
console.log("done", done);
controller.close();
return;
}
// Get the data and send it to the browser via the controller
controller.enqueue(value);
// Check chunks by logging to the console
console.log(done, value);
push();
});
}
push();
},
});
})
.then((stream) => {
return fetch(url, {
method: "POST",
headers: { "Content-Type": "image/jpeg" },
body: stream,
});
})
.then((result) => {
// Do things with result
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment