How to Convert Base64 to Image in Node.js?

10-Oct-2022

.

Admin

How to Convert Base64 to Image in Node.js?

Hey Developer,

This tutorial will provide an example of how to convert base64 to an image in node js. I explained simply about node js convert base64 to image. if you want to see an example of node js convert base64 string to image then you are in the right place. We will use node js decode base64 image. follow the below example for node.js to convert base64 to an image.

In this example, I will take one "imageData" variable with base64 string and then I will convert it into the image 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.

server.js

const fs = require("fs");

// Create Variable with Base64 Image String

var imageString = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAA...";

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:

I hope it can help you...

#Node JS