【问题标题】:How to store dynamic image at client side如何在客户端存储动态图像
【发布时间】:2013-12-13 16:24:52
【问题描述】:

首先我很抱歉如果我问以前问过哪个,但实际上我什么都没得到。
我的asp.net 页面上有一些<Div>。并使用 Javascript 我从 Url 分配背景图像。下面是代码

 divFloor.style.backgroundImage = "url(Images/FloorPlan/" + hdnFloorImgSplit[1] + ")";

hdnFloorImgSplit 是包含图片url的数组。这是因为我使用的是 Azure 云服务。
当客户端刷新页面或回发页面时,每次都会下载图像。 我想要的是,我想将它存储在客户端浏览器上并从那里使用它(如果存在)。这将节省服务器和客户端的带宽,并且速度会显着提高。

抱歉,我找不到路。我有很多图像要存储并重试。因此,我的网站变得越来越慢。 任何帮助表示赞赏

【问题讨论】:

    标签: javascript jquery html asp.net azure


    【解决方案1】:

    Blade,他们有很多不同的方法来做到这一点。我给你看一个 .With localstorage Json db。 您可以使用另一种方法,例如带有 XMLHttpRequest Level 2 的 Blob。 有关它的更多信息,您可以查看此链接 -> Saving images and filesin localStorage - javascript

    存储图片(您首先需要使用 localStorage.SetItem 存储所有图片 ....)

    这里的想法是能够获取已经加载到当前网页的图像并将其存储到localStorage。正如我们上面建立的,localStorage 只支持字符串,所以我们这里需要做的就是把图片变成一个Data URL。对图像执行此操作的一种方法是加载到画布元素中。然后,使用画布,您可以将画布中的当前视觉表示读出为数据 URL。

    让我们看看这个例子,我们在文档中有一个 id 为“elephant”的图像:

        // Get a reference to the image element
        var elephant = document.getElementById("elephant");
    
        // Take action when the image has loaded
        elephant.addEventListener("load", function () {
        var imgCanvas = document.createElement("canvas"),
        imgContext = imgCanvas.getContext("2d");
    
        // Make sure canvas is as big as the picture
        imgCanvas.width = elephant.width;
        imgCanvas.height = elephant.height;
    
        // Draw image into canvas element
        imgContext.drawImage(elephant, 0, 0, elephant.width, elephant.height);
    
        // Get canvas contents as a data URL
        var imgAsDataURL = imgCanvas.toDataURL("image/png");
    
        // Save image into localStorage
        try {
            localStorage.setItem("elephant", imgAsDataURL);
        }
        catch (e) {
            console.log("Storage failed: " + e);
        }
    }, false); 
    

    然后,如果我们想更进一步,我们可以利用 JavaScript 对象并使用 localStorage 进行日期检查。在这个例子中,我们第一次通过 JavaScript 从服务器加载图像,但之后每次页面加载,我们都从 localStorage 读取保存的图像:

    HTML

    <figure>
        <img id="elephant" src="about:blank" alt="A close up of an elephant">
        <noscript>
            <img src="elephant.png" alt="A close up of an elephant">
        </noscript>    
        <figcaption>A mighty big elephant, and mighty close too!</figcaption>
    </figure>
    

    JAVASCRIPT

    // localStorage with image
    var storageFiles = JSON.parse(localStorage.getItem("storageFiles")) || {},
        elephant = document.getElementById("elephant"),
        storageFilesDate = storageFiles.date,
        date = new Date(),
        todaysDate = (date.getMonth() + 1).toString() + date.getDate().toString();
    
    // Compare date and create localStorage if it's not existing/too old   
    if (typeof storageFilesDate === "undefined" || storageFilesDate < todaysDate) {
        // Take action when the image has loaded
        elephant.addEventListener("load", function () {
            var imgCanvas = document.createElement("canvas"),
                imgContext = imgCanvas.getContext("2d");
    
            // Make sure canvas is as big as the picture
            imgCanvas.width = elephant.width;
            imgCanvas.height = elephant.height;
    
            // Draw image into canvas element
            imgContext.drawImage(elephant, 0, 0, elephant.width, elephant.height);
    
            // Save image as a data URL
            storageFiles.elephant = imgCanvas.toDataURL("image/png");
    
            // Set date for localStorage
            storageFiles.date = todaysDate;
    
            // Save as JSON in localStorage
            try {
                localStorage.setItem("storageFiles", JSON.stringify(storageFiles));
            }
            catch (e) {
                console.log("Storage failed: " + e);
            }
        }, false);
    
        // Set initial image src    
        elephant.setAttribute("src", "elephant.png");
    }
    else {
        // Use image from localStorage
        elephant.setAttribute("src", storageFiles.elephant);
    }
    

    【讨论】:

    • 这很好,但 LocalStorage 有大小限制 从 iOS 5.1 开始,Safari Mobile 将 localStorage 数据存储在缓存文件夹中,根据操作系统的要求,该文件夹偶尔会被清理,通常是在空间不足的情况下是短。 Safari Mobile 的隐私浏览模式也完全阻止写入 localStorage。所以基本上 localStorage 跨浏览器的处理方式完全不同,我建议 indexdb 并将它们存储为博客。
    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 2020-07-19
    • 2012-03-12
    • 2021-06-24
    • 1970-01-01
    • 2017-05-28
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多