Stevan Tosic
Stevan Tosic

Reputation: 7199

Create image file in nodeJs from blob

I am receiving BLOB data on nodeJs server which is converted from PNG image.

I need to create again png image on nodeJs server to be able to show it on pdf document.

I had tried to use FileSaver on nodeJs but it is not working. FileSaver works well on reactJs app.

How can I save a new file to the local directory on the server?

There is a lot question pointing on problems with creating an image file form blob but I was unable to use base64encode, so other questions were not helpful.

Upvotes: 13

Views: 36444

Answers (2)

Stevan Tosic
Stevan Tosic

Reputation: 7199

In BLOB data of png image file there is buffer property.

So I used this solution to create image.

var imageBuffer = request.file.buffer;
var imageName = 'public/images/map.png';

fs.createWriteStream(imageName).write(imageBuffer);

This has solved my problem.

Upvotes: 16

Jit Dhar
Jit Dhar

Reputation: 192

var base64Data = req.body.image.replace(/^data:image\/png;base64,/, "");

require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
  console.log(err);
});

Try this one here image is the name on which data is coming.

Upvotes: 5

Related Questions