有没有办法增加Google Chrome中localStorage的大小以避免QUOTA_EXCEEDED_ERR:DOM例外22

HM2*_*M2K 20 javascript html5 google-chrome local-storage

我编写了一个webapp,允许您将图像存储在localStorage中,直到您点击保存(因此它可以脱机工作,如果信号很差).

当localStorage达到5MB时,Google Chrome会在javascript控制台日志中产生错误:

未捕获错误:QUOTA_EXCEEDED_ERR:DOM异常22

如何在Google Chrome上增加localStorage配额的大小?

Liz*_*aly 11

你不能,它的硬连线为5MB.这是Chrome开发人员设计决策.

在Chrome中,默认情况下,Web SQL数据库和缓存清单也有较低的限制,但如果您为Chrome App Store打包应用程序,则可以增加它们.

另请参阅管理HTML5离线存储 - Google Chrome.

  • 我对chrome开发人员非常失望 (38认同)
  • 这仍然是真的吗?!所以仍然是5MB本地存储的限制?! (6认同)
  • 5MB,UTF16编码.所以environ 2.5 M字符. (2认同)

Paw*_*wel 9

5MB是一个硬限制,这是愚蠢的.IndexedDB为您提供~50MB,这更合理.为了更容易使用,请尝试Dexie.js https://github.com/dfahlander/Dexie.js

更新:

Dexie.js实际上对我的简单键值目的来说仍然是一种矫枉过正,因此我编写了这个更简单的脚本https://github.com/DVLP/localStorageDB

有这个你有50MB,可以得到和设置这样的值

// Setting values
ldb.set('nameGoesHere', 'value goes here');

// Getting values - callback is required because the data is being retrieved asynchronously:
ldb.get('nameGoesHere', function (value) {
  console.log('And the value is', value);
});
Run Code Online (Sandbox Code Playgroud)

复制/粘贴下面这样的线ldb.set()ldb.get()从上面的例子也可以使用.

!function(){function e(t,o){return n?void(n.transaction("s").objectStore("s").get(t).onsuccess=function(e){var t=e.target.result&&e.target.result.v||null;o(t)}):void setTimeout(function(){e(t,o)},100)}var t=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB;if(!t)return void console.error("indexDB not supported");var n,o={k:"",v:""},r=t.open("d2",1);r.onsuccess=function(e){n=this.result},r.onerror=function(e){console.error("indexedDB request error"),console.log(e)},r.onupgradeneeded=function(e){n=null;var t=e.target.result.createObjectStore("s",{keyPath:"k"});t.transaction.oncomplete=function(e){n=e.target.db}},window.ldb={get:e,set:function(e,t){o.k=e,o.v=t,n.transaction("s","readwrite").objectStore("s").put(o)}}}();
Run Code Online (Sandbox Code Playgroud)

  • 这太棒了 (3认同)