在本地存储中获取并存储64mb mp4文件

问题描述:

我需要下载6个大小为64mb的视频,并将其存储在localStorage中以避免使用数据(不是WiFi)再次下载。问题是,我执行以下代码:在本地存储中获取并存储64mb mp4文件

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的视频,它抛出我的以下错误:

Storage failed: QuotaExceededError: Failed to execute 'setItem' on 'Storage': Setting the value of 'video' exceeded the quota.

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

+0

这是一个规模问题:http://*.com/a/2989317/3629438 – acupajoe

+0

你不能对你的客户端存储有很大的帮助^^。 –

+0

LocalStorage对于二进制数据是错误的。 LocalStorage仅用于小键/值对。 IndexedDB或FileSystem更适合在客户端存储Blob。 LocalStorage的大小是有限的... IndexedDB和文件系统可以请求更多配额的权限 – Endless

您可以用--unlimited-storage标志设置启动铬,铬。


但要注意,因为--unlimited-storage设置可以在镀铬,铬,如果你使用这些浏览器可以替代使用requestFileSystemFileReader创建data URI

​​3210

商店在LocalFileSystem而不是localStorage作为一个字符串File对象。

+0

“downvote”说明? – guest271314

+0

所以我无法保存64MB(约)的mp4文件?我在*中看到其他问题,说localStorage只能存储一定数量的数据(10mb)。但我应该将其部署到生产环境中。 – Anargu

+0

答案建议使用'requestFileSystem',它只在铬,铬,没有polyfill时可用。还需要使用'--unlimited-storage'设置启动铬或铬。另一种选择是用户将文件下载到用户文件系统,请参阅[如何解决下载大尺寸JSON时未捕获的RangeError](http://*.com/questions/39959467/how-to-solve-uncaught-rangeerror-when -download-large-size-json /) – guest271314