最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loading an image to localStorage and setting an image src to that location - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question edited Jul 20, 2012 at 16:37 Jones 1,50019 silver badges35 bronze badges asked Jul 20, 2012 at 15:37 NodeNodeNodeNodeNodeNode 2151 gold badge3 silver badges9 bronze badges 3
  • 1 Why exactly you want do do that? Is browser cache not enough to keep your images on host machine? – Marcelo De Zen Commented Jul 20, 2012 at 15:41
  • I want the user to be able to change the content on a page (i.e. I want the user to upload their image, not mine, and then have that image appear on the page) – NodeNodeNode Commented Jul 20, 2012 at 15:55
  • I get it! Mayge you can convert the image to base64 and then put in src="data:image/png;<..base64 image data...>". You will must use HTML5 file api to read the image file then convert the data to base64. This post will help you on convertion: stackoverflow./questions/246801/… – Marcelo De Zen Commented Jul 20, 2012 at 16:08
Add a ment  | 

1 Answer 1

Reset to default 12

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/8V9w6/ - select an image file, see the thumbnail? Then reload the page. The thumbnail should remain there. (Works latest Chrome/Firefox)

发布评论

评论列表(0)

  1. 暂无评论