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

限于喜欢 提交于 2020-01-20 19:55:10

问题


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!


回答1:


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)



来源:https://stackoverflow.com/questions/11582556/loading-an-image-to-localstorage-and-setting-an-image-src-to-that-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!