简体   繁体   中英

Convert Base64 image back to binary to write a file

I've been working on an Image Caching system for my mobile application, and currently it's receiving a base64 image from my server, and saving that base64 encoding in a text file. Whenever the image needs to be used the file is loaded, and the <img src=''> is set to the base64 encoding text that was inside of the file.

I'm wondering if it's possible, to instead of saving the files as img.txt and having base64 encoding inside of them, if I can take the base64 encoding, convert it back into binary, and write it into the file as text.

Something like this:

var image   = getBase64FromServer();
var dir     = FileSystem.dataDirectory + "image.jpg";
var text    = getBinary(image);
var replace = true;

FileSystem.writeFile(directory, text, replace).then(function(success) {
    console.log("File created");
}, function(error) {
    console.log("Error creating file: " + error);
});

This way I can just set my <img src=''> to something like this:

<img src="file:///var/mobile/Containers/Data/Application/x/Library/NoCloud/image.jpg">

and have it load, opposed to using the larger base64 alternative.

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