I wanted to store a user's uploaded image into localStorage, so I can provide a solution for this situation. I got this to work with a combination of the accepted answer, James H. Kelly's comment, and with reference to the Mozilla docs.
I first stored the uploaded image as a Base64 string using a FileReader object:
// get user's uploaded image
const imgPath = document.querySelector('input[type=file]').files[0];
const reader = new FileReader();
reader.addEventListener("load", function () {
// convert image file to base64 string and save to localStorage
localStorage.setItem("image", reader.result);
}, false);
if (imgPath) {
reader.readAsDataURL(imgPath);
}
Then, to read the image back in from localStorage, I simply did the following:
let img = document.getElementById('image');
img.src = localStorage.getItem('image');