-
-
Save madhums/e749dca107e26d72b64d to your computer and use it in GitHub Desktop.
/* | |
* Taken from http://stackoverflow.com/questions/5867534/how-to-save-canvas-data-to-file/5971674#5971674 | |
*/ | |
var fs = require('fs'); | |
// string generated by canvas.toDataURL() | |
var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0" | |
+ "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO" | |
+ "3gAAAABJRU5ErkJggg=="; | |
// strip off the data: url prefix to get just the base64-encoded bytes | |
var data = img.replace(/^data:image\/\w+;base64,/, ""); | |
var buf = new Buffer(data, 'base64'); | |
fs.writeFile('image.png', buf); |
Hi,
Above mentioned solution is great.
Can it be possible to store the image in temporary location? I don't want to save the image file in permanent storage.
Struggling and found this (still) works... Thank you...
This is working fine.Thank you very much
@ooade
Line:
var buf = new Buffer(img, 'base64');
should be:
var buf = new Buffer(data, 'base64');
Thanks. Please what is "fs"?
@neilbannet fs is a node file-system module. read more about fs here https://nodejs.org/api/fs.html#fs_file_system
This saved my butt! Thank you
cmt_file is variable which has base64 encoded image and i want to store it as file in my folder as you people have given example i have tried but it throwing me error ..here is my code....:-
var ext = cmt_img.split(';')[0].match(/jpeg|jpg|png|gif/)[0];
var data_img = cmt_img.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data_img, 'base64');
fs.writeFile('img_func.jpg',buf,function(err) {
console.log(err);
});
it is throws me error-
Error
at readStream (/opt/lampp/htdocs/XXXX/node/node_modules/raw-body/index.js:196:17)
at getRawBody (/opt/lampp/htdocs/XXXX/node/node_modules/raw-body/index.js:106:12)
at read (/opt/lampp/htdocs/XXXX/node/node_modules/body-parser/lib/read.js:76:3)
at urlencodedParser (/opt/lampp/htdocs/XXXX/node/node_modules/body-parser/lib/types/urlencoded.js:115:5)
at Layer.handle [as handle_request] (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:317:13)
at /opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:335:12)
at next (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/router/index.js:275:10)
at expressInit (/opt/lampp/htdocs/XXXX/node/node_modules/express/lib/middleware/init.js:40:5)
so where is the problew can anyone help
👍
good
thanks
very good!
how to save this image in excel? Thanks Padam
I got a Buffer deprecated warning in NodeJS. I replaced with Buffer.from and it work. Also fixed a callback function warning:
// string generated by canvas.toDataURL()
var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
+ "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
+ "3gAAAABJRU5ErkJggg==";
// strip off the data: url prefix to get just the base64-encoded bytes
var data = img.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer.from(data, 'base64');
fs.writeFile('image.png', buf, err => {
if (err) throw err;
console.log('Saved!');
});
is it secure?
this is great
why do I always have error "Buffer is not defined"??
Thanks! This should do for those having image error