0

I need to download six videos of a size 64mb roughly, store them in localStorage to avoid using data (not wifi) to download it again. The problem is that I executed the following code:

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();
}

After testing this script I could see from the browser that the mp4 file was downloading but when it finished the task to save the video in localStorage, it throws me the following error:

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

How can I store these files in localStorage?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Anargu
  • 517
  • 2
  • 8
  • 17
  • It's a size issue: http://stackoverflow.com/a/2989317/3629438 – acupofjose Nov 08 '16 at 22:15
  • 1
    You cannot save something that big to your client storage ^^. – Valentin Roudge Nov 08 '16 at 22:17
  • LocalStorage is wrong for binary data. LocalStorage is only meant to be used with small key/value pair. IndexedDB or FileSystem is more suitable for storing blobs in client side. LocalStorage size is limited... IndexedDB and filesystem can ask for permission for more quota – Endless Nov 09 '16 at 16:32
  • I suggest you take a look at the [cache api](https://developer.mozilla.org/en-US/docs/Web/API/Cache) – Endless Nov 09 '16 at 16:36

1 Answers1

1

You can launch chrome, chromium with --unlimited-storage flag set.


Though note, since --unlimited-storage setting is possible at chrome, chromium, if you use those browsers you can substitute using requestFileSystem for data URI created by FileReader.

Length limitations

Although Firefox supports data URIs of essentially unlimited length, browsers are not required to support any particular maximum length of data. For example, the Opera 11 browser limited data URIs to around 65000 characters.

Store File object at LocalFileSystem instead of localStorage as a string.

See

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • So I can't save a mp4 file of 64mb(approx) ? I looked at other questions in stackoverflow that says localStorage only can store a certain amount of data (10mb). But I should deploy this to production. – Anargu Nov 09 '16 at 01:39
  • The Answer suggest to use `requestFileSystem`, which is only available at chrome, chromium, without a polyfill. Also requires launching chrome, or chromium with `--unlimited-storage` set. Another alternative would be for user to download file to user filesystem, see [How to solve Uncaught RangeError when download large size json](http://stackoverflow.com/questions/39959467/how-to-solve-uncaught-rangeerror-when-download-large-size-json/) – guest271314 Nov 09 '16 at 01:44
  • _"So I can't save a mp4 file of 64mb(approx) ?"_ Have not yet found a workaround that addresses both possible `Storage` limitation and `data URI` string `.length` limitation. – guest271314 Nov 09 '16 at 02:01
  • I thought this issue was an older problem which could get someone then it would have been resolved. So is there still not a documentation about this issue (storage limitation...)? – Anargu Nov 09 '16 at 02:34
  • Yes, there is documentation. At chrome, chromium you can check storage quota http://stackoverflow.com/questions/10477489/what-are-the-details-can-be-obtained-from-webkitstorageinfo-queryusageandquota/10478733#10478733 ; or set `--unlimited-storage` quota. See https://developers.google.com/web/updates/2011/11/Quota-Management-API-Fast-Facts, https://groups.google.com/a/chromium.org/forum/#!msg/chromium-html5/m-ei3ATZr2c/ZkO6ZkLSYVIJ , at firefox navigate to `about:config` and check `dom.storage.default_quota` https://arty.name/localstorage.html – guest271314 Nov 09 '16 at 02:55
  • To avoid the issue, you can offer file for download, see link at second comment above, then prompt user to upload the file or folder for use at the document user is visiting, see http://stackoverflow.com/questions/39664662/how-to-upload-and-list-directories-at-firefox-and-chrome-chromium-using-change-a – guest271314 Nov 09 '16 at 03:14