Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image to localStorage and setting an image src to that location

I've successfully allowed for a user to upload an image to local storage but I want to be able to then take that image and fill an image element on the page with that.

<h3>Please upload the image you wish to use.</h3>
<input id="photoin" type="file" accept="image/*;capture=camera"></input>
<a href="#delete_1" data-role="button" onClick="save()">Submit</a>

<script>
    var i = 0; 
    function save(){                
        var input = document.getElementById("photoin").value; 
        var name = "photo_" + i; 
        localStorage.setItem(name, input);      
        $("#QR").src = window.localStorage[name]; 
        i++; 
    }
</script>

I'm looking for something that will successfully give me the URL of the image in storage, seeing as "window.localStorage[name]" only returns the value paired with that key.

Thanks!

like image 424
NodeNodeNode Avatar asked Jul 20 '12 15:07

NodeNodeNode


People also ask

How do I add an image to local storage?

To save an image to localStorage and display it on the next page with JavaScript, we can create a canvas, put the image in it, and then convert the canvas data to a base64 string. const getBase64Image = (img) => { const canvas = document. createElement("canvas"); canvas. width = img.

How can I save an image from IMG tag?

This is how you do it: var save = document. createElement('a'); save. href = $('#myImageID').

How do I load localStorage?

To get items from localStorage, use the getItem() method. getItem() allows you to access the data stored in the browser's localStorage object.

How to load image from localStorage on second page?

Get the localStorage on the second page; try something like this: window.onload = function () { var picture = localStorage.getItem ('image'); var image = document.createElement ('img'); image.src = picture; document.body.appendChild (image); }; Then what would I place on the next page to load the image?

How to convert an image to a data URL in localStorage?

As we established above, localStorage only supports strings, so what we need to do here is turn the image into a Data URL. One way to do this for an image, is to load into a canvas element.

Is it possible to save a string to localStorage?

But since localStorage is just string-based, saving a long string with no form of structure isn’t optimal. The idea here is to be able to take an image that has been loaded into the current web page and store it into localStorage.

How to add images to the Dom and reload the page?

After adding the image to the DOM, this function calls saveImages () and then window.location.reload (). Because of that, it is basically superfluous to add the image to the DOM and then reload the page. That function could basically be reduced to the following, utilizing the loadStorage () function to add the images.


1 Answers

Well you can store the actual image data in localStorage (though be wary - there's a limit)

Have a look at the HTML5 rocks tutorial & scroll down to the bit headed READING FILES

Here the file is being read then the output put in the img:src tag. You could additionally put it in localStorage

Example: http://jsfiddle.net/8V9w6/ - select an image file, see the thumbnail? Then reload the page. The thumbnail should remain there. (Works latest Chrome/Firefox)

like image 104
paulslater19 Avatar answered Nov 12 '22 17:11

paulslater19