weixin_33709364 2012-11-26 09:24 采纳率: 0%
浏览 165

将图像存储在localStorage中

I try to stock an image in the localStorage using an Ajax request.

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <script type="text/javascript">
            window.onload = function() {
                var xmlhttp;
                if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                        var storedImage;
                        if (localStorage.getItem('imgtest')) {
                            storedImage = localStorage.getItem('imgtest');
                            alert("get");
                        }
                        else {
                            storedImage = xmlhttp.responseText;
                            localStorage.setItem('imgtest',storedImage);
                            alert("set");
                        }
                        document.getElementById("test").style.backgroundImage = 'data:image/png;base64,' + storedImage;
                    }
                }
                xmlhttp.open("GET","img.png");
                xmlhttp.setRequestHeader("Content-type","image/png");
                xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <div id="test" style="width: 100px; height: 100px;"></div>
    </body>
</html>

I can store the stream, but the image is not displayed.

Maybe it is because of my encoding, I don't know. Can you tell me what I do wrong or if there is a better way to do this ?

  • 写回答

2条回答 默认 最新

  • weixin_33749242 2012-12-05 14:50
    关注

    Did you try to do your request with POST ?

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?