javascript localStorage 存储大尺寸数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17255786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
localStorage store large size data
提问by user2489547
I want to use localStorage to store large amount of data(like 800GB), according to http://arty.name/localstorage.htmland also I'm using Firefox, I changed my localStorage size and also the cache size. So the size isn't a problem. However, I write some jquery like the following:
我想使用 localStorage 来存储大量数据(如 800GB),根据http://arty.name/localstorage.html并且我正在使用 Firefox,我更改了我的 localStorage 大小和缓存大小。所以大小不是问题。但是,我编写了一些 jquery,如下所示:
$("a[href]").each(function(){
$(this).click(function(event){
localStorage.test += "somenewinformation";
...
If this localStorage.test already have large amount of data like 400GB, so the storing information step would be extremely slow. When I click on a link,will the jquery wait for me to finish the appending new information to localStorage.test or it will just go to the next page and information in localStorage.test will all lost or localStorage.test will just remain the old value? What I dont understand is whether a new thread will be generated to do this storing in background or not and closing browser in the middle will affect it or not.
如果这个localStorage.test已经有400GB这样的大量数据,那么存储信息的步骤会非常慢。当我点击一个链接时,jquery 会等待我完成将新信息附加到 localStorage.test 还是它会直接转到下一页并且 localStorage.test 中的信息将全部丢失或 localStorage.test 将保持旧的价值?我不明白的是是否会生成一个新线程来在后台执行此存储以及在中间关闭浏览器是否会影响它。
Sorry about the messy description and thanks in advance!
对凌乱的描述深表歉意,并提前致谢!
回答by
You can't! Usual limit is 5 mb.
你不能!通常的限制是 5 mb。
Some browser such as Opera allow you to adjust the size, but this is purely dependent on browser and is a user initiated action, not a programmable one.
某些浏览器(例如 Opera)允许您调整大小,但这完全取决于浏览器并且是用户启动的操作,而不是可编程的操作。
And even if you could, remember that localStorage
can only store strings so anything else need to be stringified first. That together with this being an key/value storage array you will run into pretty poor performance at the end.
即使可以,请记住它localStorage
只能存储字符串,因此其他任何内容都需要先进行字符串化。再加上这是一个键/值存储阵列,你最终会遇到非常糟糕的性能。
If you need large storage capacity, look into File API instead. This is made to work with large files (Blobs) and you can store anything as a Blob.
如果您需要大存储容量,请查看 File API。这是用于处理大文件 (Blob) 的,您可以将任何内容存储为 Blob。
800 Gb is a large size and File API can only work as fast as the file system (at best, more likely a bit slower as it is sandboxed, ie. not a pure file system).
800 Gb 是一个很大的大小,文件 API 的运行速度只能与文件系统一样快(充其量,因为它是沙盒的,所以很可能会慢一点,即不是纯文件系统)。
More about File System API (Note: discontinued as of 4/2014):
http://www.w3.org/TR/file-system-api/
有关文件系统 API 的更多信息(注意:自 4/2014 起停产):http:
//www.w3.org/TR/file-system-api/
Tutorial:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
教程:http:
//www.html5rocks.com/en/tutorials/file/dndfiles/
Blob:
https://developer.mozilla.org/en-US/docs/Web/API/Blob
Blob:https:
//developer.mozilla.org/en-US/docs/Web/API/Blob
UpdateAs the Filesystem API has been discontinued there are essentially only two options left (besides from storing data to the server):
更新由于 Filesystem API 已停止使用,基本上只剩下两个选项(除了将数据存储到服务器):
- Indexed Database APIaka
Indexed DB
(recommended) - Web SQL (deprecated but still supported in browsers such as Safari)
- 索引数据库 API又名
Indexed DB
(推荐) - Web SQL(已弃用,但在 Safari 等浏览器中仍受支持)
Also these are initially limited in size by the browser. It is possible to request a larger quote which the user is asked to accept.
此外,这些最初受浏览器的大小限制。可以要求用户接受更大的报价。
See more details about this API here:
http://www.w3.org/TR/IndexedDB/
在此处查看有关此 API 的更多详细信息:http:
//www.w3.org/TR/IndexedDB/
回答by Matt Crinklaw-Vogt
There is LargeLocalStoragewhich provides a cross-browser way to storage large amounts of data locally. You can store binary data or strings.
有LargeLocalStorage它提供了一个跨浏览器的方式来存储大量数据的本地。您可以存储二进制数据或字符串。
LargeLocalStorage uses the FilesystemAPIon Chrome, IndexedDBin IE and Firefox and WebSQLin Safari.
LargeLocalStorage在 Chrome 上使用FilesystemAPI,在 IE 和 Firefox 中使用IndexedDB,在 Safari 中使用WebSQL。