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.

1

2 Answers 2

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.

1
  • 5
    Apparently property 'buffer' does not exist on type 'Blob'
    – Phil
    Commented Aug 13, 2021 at 12:52
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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