How to store the data into a file for a jpeg that has been fetched successfully in nodejs ? | Sololearn: Learn to code for FREE!

0

How to store the data into a file for a jpeg that has been fetched successfully in nodejs ?

8/12/2022 1:23:04 PM

Abhay

8 Answers

New Answer

+3

What data do you have? And from/where you wanna store them? Image data use to be store in database as byte arrays then you convert them into string and send to client where it is set as image source

+2

<DD> ty for your response, Don't know what data it is . But i am using fetch to get the image ,now i want to store whatever image data(in bytes or any other format) fetch function send us, in a jpeg file . Fyi , it is not about sending to a client or anyone else . I could download the file through browser but just want to see how it can be done using node js.

+2

Abhay I would say it makes no sense to try download image using nodejs, streams are used to write/read files between client and server, you should simply fetch the image then download it using normal js

+2

Canvas is one of options as Mirielle mentioned, check all the possible ways here: https://stackoverflow.com/questions/6150289/how-can-i-convert-an-image-into-base64-string-using-javascript

+1

Abhay Ohh, I get you now, can you share the code, I can revise it easier than writing the whole thing

0

<DD> my code is a mess ,anyway i tried writing something by following this link and going through other articles ,but didn't understand how to do it. https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeTo The above article explains how to use "pipeto" to direct readableStream to writeableStream but i am unable to create writeableStream . Though ,I coded a simple writestream but it didn't work out . let writeStream = fs.createWriteStream("something.jpeg" ); Error : TypeError [ERR_INVALID_ARG_TYPE]: The "transform.writable" property must be an instance of WritableStream. Received an instance of WriteStream.

0

<DD> not interested in doing that but thks.

0

The question is VAGUE! But the comment shows that you want to store image data which can be a large array containing pixel values. The easiest method is to explore the fetch response by printing it to the console, I believe there's a handy function returned that makes it easy to retrieve image data The hard method is to send the whole image to the client, and using html5 canvas, you can use the image data to get the data, you can then send this data back to the server...