.Literfieber
.Literfieber•2y ago

How to transform a Readable Stream to a file.

I have a ReadableStream from a post request and i cant find a way to transform the body(ReadableStream) to a file. I tried to pipe the ReadableStream into a file like this const file = await Deno.open('./test.png', {create: true,write: true,}) myPostRequest.body.pipeTo(file.writable); file.close(); I always get: error: Uncaught (in promise) BadResource: Bad resource ID at async write (deno:ext/net/01_net.js:33:12) at async Object.write (deno:ext/net/01_net.js:82:25) what is the correct way to create a file from a readable stream ?
2 Replies
crowlKats
crowlKats•2y ago
pipeTo is async, so you'd have to wait for it to complete before closing the file. also, you don't need to close the file manually, the pipeTo will do that automatically
.Literfieber
.Literfieber•2y ago
That worked but now it seems to have the wrong encoding I cant open it in a img viewer It seems like it is posted as multipart/form-data it might has some extra data in it that corrupts the output Ok i found the solution thanks for the help @crowlkats 😄 It seems like i had to parse it as multipart/form-data and then use the data content to create the file via Deno.writeFile()