Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 784
Stevan Tosic Avatar asked Aug 06 '18 13:08

Stevan Tosic


People also ask

How do I convert a Blob file to an image?

getContext("2d"); // Get the "context" of the canvas var img = document. getElementById("myimage"); // The id of your image container ctx. drawImage(img,0,0,width,height); // Draw your image to the canvas var jpegFile = canvas. toDataURL("image/jpeg"); // This will save your image as a //jpeg file in the base64 format.

How do I convert a Blob to a file?

To convert Blob to File in JavaScript, we can use the File constructor. const file = new File([myBlob], "name"); to create a File object with the myBlob blob object in the array. The 2nd argument is the file name string.

How do I write an image in node JS?

To write an image to a local server with Node. js, we can use the fs. writeFile method. to call res.


1 Answers

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.

like image 95
Jit Dhar Avatar answered Sep 18 '22 12:09

Jit Dhar