13

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.

Stevan Tosic
  • 6,561
  • 10
  • 55
  • 110
  • Possible duplicate of [NodeJS: Saving a base64-encoded image to disk](https://stackoverflow.com/questions/6926016/nodejs-saving-a-base64-encoded-image-to-disk) – Jit Dhar Aug 06 '18 at 14:23

2 Answers2

16

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.

Stevan Tosic
  • 6,561
  • 10
  • 55
  • 110
5
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.

Jit Dhar
  • 192
  • 10