I was wondering if it is possible to

I was wondering if it is possible to pipe an (image) stream into FormData? I currently have this setup:
const { readable, writable } = new TransformStream();
req.body.pipeTo(writable);

const formData = new FormData();
formData.append(
"file",
readable,
"test.jpg"
);

const response = await fetch(
"...",
{
method: "POST",
body: formData
}
);
const { readable, writable } = new TransformStream();
req.body.pipeTo(writable);

const formData = new FormData();
formData.append(
"file",
readable,
"test.jpg"
);

const response = await fetch(
"...",
{
method: "POST",
body: formData
}
);
Which does not work - at least when uploading the image to Cloudflare Images, it says that the image has an incorrect mime type. If I read the whole body and append it into the form data, it does work as expected - but since that obviously isn't recommended because of the memory limit, I don't like that approach. I couldn't find anything on the interwebz
2 Replies
Manzoor Wani
Manzoor Wani7mo ago
Hey, did you manage to solve this problem? I am trying to solve as similar problem https://discord.com/channels/595317990191398933/1230170578674253886
Fabian
Fabian7mo ago
I think I was told that it's not possible but it should be no issue reading the whole body in my case since the files are <10MB. But yeah the streaming thing is generally an issue, I recently got stuck wanting to calculate a checksum of a stream and came to the conclusion with all of the current browser APIs and no additional APIs from Workers it's impossible if you don't read the whole body. I understand that everything should be streamed, since it makes sense, but if Cloudflare wants us to force using streams, then they should provide support for such stuff. I don't think this and my new cryptography problem are uncommon use-cases and they sadly will break if the incoming data is too big because of the memory limits. Granted: Upload limit is generally 100MB if you don't have Cloudflare Enterprise, but you can obviously donwload files from an external API that are bigger than 100MB.
Want results from more Discord servers?
Add your server