Kirk Ouimet
Kirk Ouimet

Reputation: 28374

JPEG File Encoding and writeFile in Node JS

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

Upvotes: 0

Views: 1888

Answers (2)

DWAXGIO
DWAXGIO

Reputation: 1

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);
      }
    }
  );

Upvotes: 0

Kirk Ouimet
Kirk Ouimet

Reputation: 28374

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

Upvotes: 1

Related Questions