0

I'm using http.request to download JPEG file. I am then using fs.writeFile to try to write the JPEG file out to the hard drive.

None of my JPEG files can be opened, they all show an error (but they do have a file size). I have tried all of the different encodings with fs.writeFile.

What am I messing up in this process?

Here's what the working one is showing when viewing it raw: enter image description here

And here is what the bad one using fs.writeFile is showing: enter image description here

3
  • 1
    possible duplicate of nodejs write raw image data to jpeg file?
    – Jordonias
    Aug 22, 2014 at 17:23
  • I'm trying to get fs.writeFile to work, those other answers to not address this Aug 22, 2014 at 17:26
  • Can you post the code you are using to write to the disk? While .setEncoding may solve your problem, there is likely a better way because that encoding is not recommended in most cases. Aug 22, 2014 at 20:53

2 Answers 2

1

Figured it out, needed to use res.setEncoding('binary'); on my http.request.

0

Thank you, looking to the previous response, I was able to save de media correctly:

fs.writeFile(
    filepath + fileName + extension,
    mediaReceived, // to use with writeFile       
    { encoding: "binary" }, // to use with writeFile ***************WORKING
    (err) => {
      if (err) {
        console.log("An error ocurred while writing the media file.");
        return console.log(err);
      }
    }
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.