简体   繁体   中英

How can I save image as base 64?

I am using phantom js to retrieve the image's strings and then I encode them with base 64.

var content = btoa(unescape(encodeURIComponent(imagestring)));
self.writeFile(pathToFolder + fileTitle, content);      

But images are not displayed. It says they are damaged. How can I save an image like that?

If you want save image as base64 with JavaScript, you can try with this code:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM