【问题标题】:Fetching and Storing a 64mb mp4 file in localStorage在 localStorage 中获取和存储 64mb mp4 文件
【发布时间】:2017-03-22 16:38:50
【问题描述】:

我需要下载大约64mb大小的六个视频,将它们存储在localStorage中以避免使用数据(不是wifi)再次下载它。问题是我执行了以下代码:

var urlVideo = "<link_of_mp4_file>"
var videoStorage = localStorage.getItem("video"),
    videoElement = document.getElementById("video"),
    sourceElement = document.getElementById("source_video");

if (videoStorage) {
    // Reuse existing Data URL from localStorage
    sourceElement.setAttribute("src", videoStorage);
}
else {
    // Create XHR, Blob and FileReader objects
    var xhr = new XMLHttpRequest(),
        blob,
        fileReader = new FileReader();

    xhr.open("GET", urlVideo, true);
    // Set the responseType to arraybuffer. "blob" is an option too, rendering manual Blob creation unnecessary, but the support for "blob" is not widespread enough yet
    xhr.responseType = "arraybuffer";

    xhr.addEventListener("load", function () {
        if (xhr.status === 200) {
            // Create a blob from the response
            blob = new Blob([xhr.response], { type: "video/mp4" });

            // onload needed since Google Chrome doesn't support addEventListener for FileReader
            fileReader.onload = function (evt) {
                // Read out file contents as a Data URL
                var result = evt.target.result;
                // Set image src to Data URL
                videoElement.setAttribute("src", result);
                // Store Data URL in localStorage
                try {
                    localStorage.setItem("video", result);
                }
                catch (e) {
                    console.log("Storage failed: " + e);
                }
            };
            // Load blob as Data URL
            fileReader.readAsDataURL(blob);
        }
    }, false);
    // Send XHR
    xhr.send();
}

测试此脚本后,我可以从浏览器中看到 mp4 文件正在下载,但是当它完成将视频保存在 localStorage 中的任务时,它会抛出以下错误:

存储失败:QuotaExceededError:无法在“存储”上执行“setItem”:设置“视频”的值超出了配额。

如何将这些文件存储在 localStorage 中?

【问题讨论】:

  • 你不能把这么大的东西保存到你的客户端存储中^^。
  • LocalStorage 对于二进制数据是错误的。 LocalStorage 仅适用于小型键/值对。 IndexedDB 或 FileSystem 更适合在客户端存储 blob。 LocalStorage 大小有限... IndexedDB 和文件系统可以请求获得更多配额的权限
  • 我建议你看看cache api

标签: javascript html video local-storage


【解决方案1】:

您可以启动 chrome,设置了 --unlimited-storage 标志的铬。


尽管注意,由于 --unlimited-storage 可以在 chrome、chromium 上设置,如果您使用这些浏览器,您可以使用 requestFileSystem 替换由 FileReader 创建的 data URI

长度限制

虽然 Firefox 本质上支持数据 URI 长度不限,浏览器不需要支持任何特定的 数据的最大长度。例如,Opera 11 浏览器限制数据 URI 长度约为 65000 个字符。

File 对象存储在LocalFileSystem 而不是localStorage 作为字符串。

【讨论】:

  • 所以我无法保存 64mb(大约)的 mp4 文件?我查看了 stackoverflow 中的其他问题,说 localStorage 只能存储一定数量的数据(10mb)。但我应该将它部署到生产环境中。
  • 答案建议使用requestFileSystem,它只适用于chrome、chromium,没有polyfill。还需要启动 chrome 或带有 --unlimited-storage 设置的 chromium。另一种选择是让用户将文件下载到用户文件系统,请参阅How to solve Uncaught RangeError when download large size json
  • “所以我无法保存 64mb(大约)的 mp4 文件?” 尚未找到解决可能的Storage 限制和data URI 的解决方法字符串.length 限制。
  • 我认为这个问题是一个较老的问题,如果有人可以解决它。那么仍然没有关于这个问题的文档(存储限制......)?
  • 是的,有文档。在 chrome, chromium 你可以查看存储配额 stackoverflow.com/questions/10477489/… ;或设置--unlimited-storage 配额。请参阅 developers.google.com/web/updates/2011/11/…groups.google.com/a/chromium.org/forum/#!msg/chromium-html5/… ,在 firefox 导航到 about:config 并检查 dom.storage.default_quota arty.name/localstorage.html
猜你喜欢
  • 2017-08-24
  • 2020-12-29
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2020-07-13
相关资源
最近更新 更多