How do I parse a data URL in Node

0 votes

I've got a data URL like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...

What's the easiest way to get this as binary data (say, a Buffer) so I can write it to a file?

Nov 30, 2020 in Node-js by kartik
• 37,520 points
1,781 views

1 answer to this question.

0 votes

Hello @kartik,

Put the data into a Buffer using the 'base64' encoding, then write this to a file:

var fs = require('fs');
var string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
var regex = /^data:.+\/(.+);base64,(.*)$/;

var matches = string.match(regex);
var ext = matches[1];
var data = matches[2];
var buffer = Buffer.from(data, 'base64');
fs.writeFileSync('data.' + ext, buffer);

Hope it helps!!

Thank You!!

answered Nov 30, 2020 by Niroj
• 82,840 points

Related Questions In Node-js

0 votes
1 answer

How do I manage MongoDB connections in a Node.js web application?

When the Node.js application starts, create a ...READ MORE

answered Jun 10, 2022 in Node-js by Neha
• 9,020 points
1,953 views
0 votes
1 answer

How do I get the time of day in javascript/Node.js?

Hello @kartik, This function will return you the ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
1,166 views
0 votes
1 answer

How do I define methods in a Mongoose model?

Hello @kartik, You didn't specify whether you were ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,840 points
2,739 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,023 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,004 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,985 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,911 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,586 views
0 votes
1 answer

How do I URl encode something in Node.js?

Hello @kartik, The built-in module querystring is what you're looking ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,840 points
2,380 views
0 votes
1 answer

How do you log content of a JSON object in Node.js?

Hello @kartik, Try this one: console.log("Session: %j", session); If the ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
873 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP