Categories
JavaScript Answers

How to write base64 image-file with Node?

Spread the love

To write base64 image-file with Node, we call writeFile.

For instance, we write

const image = "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA..kJggg==";
const data = image.replace(/^data:image\/\w+;base64,/, "");

fs.writeFile(fileName, data, { encoding: "base64" }, (err) => {
  //...
});

to call writeFile with the MIME type part of the base64 string removed.

And we set encoding to 'base64' to convert the base64 string to binary content.

We get errors from err in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.