简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM