0

My image src is base64 data as bellow:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABd4AAAH ...." />

I want to save it as an image (like png format) by nodejs; how is it possible? I am using following code. The src is too big.and when I convert, the image is converted untill its half maybe.

var dt= "iVBORw0KGgoAAAANSUhEUgAABd4AAAH ...." 
let buff = Buffer.from(dt, 'base64');
fs.writeFileSync('./myImage.png', buff);
Harald K
  • 26,314
  • 7
  • 65
  • 111
Javad-M
  • 456
  • 6
  • 22

2 Answers2

1

You forgot to add extra parameter to write method: "encoding". And value of this parameter should be encoding you want to do, in this example "base64"

In the end, your code should look like this:

var dt= "iVBORw0KGgoAAAANSUhEUgAABd4AAAH ...." 
let buff = Buffer.from(dt, 'base64');
fs.writeFileSync('./myImage.png', buff, { encoding: "base64" });
0

It's works but you should add encoding param

var dt= "iVBORw0KGgoAAAANSUhEUgAABd4AAAH ...." 
let buff = Buffer.from(dt, 'base64');
fs.writeFileSync('./myImage.png', buff, {encoding: "base64"});

encoding parameter must be entered for correctly work.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31