1

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.

Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • See http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata , https://gist.github.com/borismus/1032746 . Another option would be to set `img` `src` as `objectURL` , if requirement is to not set `src` as `data URI` string – guest271314 Dec 11 '15 at 17:52

1 Answers1

0

Take a loot at WindowBase64.atob()