Question

I want to save an image as binary to the users localhost for future reference, then at a later date, generate a dataurl from this binary, problem is, it's not working.

var reader = new FileReader();
reader.onload = function(file){
 this.file = file.target.result;
}.bind(this);
reader.readAsBinaryString(asset);

var reader = new FileReader();
reader.onload = function(image){
 this.image = image.target.result;
}.bind(this);
reader.readAsDataURL(this.file);

This might seem a little peverse, but the image data is being put in localstorage, then at a later date (maybe a minute, maybe a week) I want to be able to generate an image from it.

Any ideas? Thanks!

Was it helpful?

Solution

It would probably be easiest to just read it as a data URL, and save that into localStorage. However, if you REALLY need the binary data, then you could just save them separately. Then, when you need to generate the image, just do something like:

var img = document.createElement("img");
img.setAttribute("src", localStorage.imageDataURL);
document.body.appendChild(img);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top