I'm having trouble finding how exactly I would store an uploaded image in localStorage, as inputted by a user in my options page. There isnt too much documentation on how to do it because for awhile it was a known error and I couldnt find any examples on Stack. There's some examples on how to save an image already on a page but nothing on an inputed image.
All i have is the HTML, but I imagine that the javascript running in the back needs to access the value of the input or something (?).
<input id="uploadImage" type="file" accept="image/*" />
Then after the user chooses a file, the value of the input is C:\fakepath\some_image.jpg
. I dont want to store the filepath but store the actual image, if possible.
$("#uploadImage").change(function(){
alert($("#uploadImage").val());
});
EDIT:
I found a sample on how to do it from this stack topic, but the filepath that I get from $("#uploadImage").val()
doesnt work (So I dont know if this method work work even if the filepath was valid)
var img = new Image();
img.src = $("#uploadImage").val();
img.load = function() {
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
var data = context.getImageData(x, y, img.width, img.height).data;
localStorage['uploadedImage'] = data; // save image data
};