Created
December 10, 2021 12:34
-
-
Save zdunecki/0a874687c308eadc22c031636025bde3 to your computer and use it in GitHub Desktop.
Send stream from blob URL to API - WIP
This file contains 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 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