12-Oct-2022
.
Admin
Hi Guys,
Hello, all! In this article, we will talk about how to convert base64 to png in node js. Here you will learn node js convert base64 to png. you can see node js convert base64 string to png image. if you want to see an example of node js decode base64 png image then you are in the right place.
In this example, i will take one "imageData" variable with base64 string and then i will convert it into png using "fs" library. so let's see the example code below:
Step 1: Install Node JS
This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:
mkdir my-app
cd my-app
npm init
Step 2: Create server.js File
You can take the below code and put into "server.js" file.
const fs = require("fs");
// Create Variable with Base64 Image String
var imageString = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCA...";
var base64Data = imageString.replace(/^data:image\/png;base64,/, "");
// Store Image into Server
fs.writeFile("image.png", base64Data, 'base64', function(err) {
console.log(err);
});
console.log("Image Saved Successfully.");
Run Node JS:
All steps have been done, now you have to type the given command and hit enter to run the node.js app:
node server.js
Output:
#Node JS