如何在不崩溃浏览器的情况下使用IndexedDB创建一个非常长的字符串? [英] How can I make a really long string using IndexedDB without crashing the browser?

查看:231
本文介绍了如何在不崩溃浏览器的情况下使用IndexedDB创建一个非常长的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Web应用程序,它会生成一个用户将下载的潜在大文本文件,并且所有处理都在浏览器中完成。到目前为止,我能够以小块的形式读取超过1 GB的文件,处理每个块,逐步生成大量输出文件,并将增长的输出存储在IndexedDB中。我更天真的尝试将所有结果保存在内存中,然后在最后将它们序列化为文件,导致所有浏览器崩溃。

I'm writing a web app that generates a potentially large text file that the user will download, and all the processing is done in the browser. So far I'm able to read a file over 1 GB in small chunks, process each chunk, generate a large output file incrementally, and store the growing output in IndexedDB. My more naïve attempt which kept all the results in memory and then serialized them to a file at the very end was causing all browsers to crash.

我的问题是双重的:


  1. 我可以追加到IndexedDB中的条目(字符串或数组)而无需读取整个内容先进入内存?现在,这个:

task.dbInputWriteQueue.push(output);
var transaction = db.transaction("files", "readwrite");
var objectStore = transaction.objectStore("files");
var request = objectStore.get(file.id);
request.onsuccess = function()
{
    request.results += nextPartOfOutput
    objectStore.put(request.results);
};

在输出开始变大后导致崩溃。我可以在数据库中写一堆小条目,但是后来我不得不将它们全部读入内存,以便将它们连接起来。请参阅我的问题的第2部分...

is causing crashes after the output starts to get big. I could just write a bunch of small entries into the database, but then I'd have to read them all in to memory later anyway to concatenate them. See part 2 of my question...

我是否可以创建数据对象URL以引用IndexedDB中的值而不将该值加载到内存中? 对于我可以做的小字符串:

Can I make a data object URL to reference a value in IndexedDB without loading that value into memory? For small strings I can do:

var url = window.URL.createObjectURL(new Blob([myString]), {type: 'text/plain'});

但对于大字符串,这并不太好。实际上,它在加载字符串之前崩溃了。似乎使用来自IndexedDB的 get()的大读取导致Chrome至少崩溃(甚至开发人员工具崩溃)。

But for large strings this doesn't jive too well. In fact, it crashes before the string is loaded. It seems that big reads using get() from IndexedDB cause Chrome, at least, to crash (even the developer tools crash).

如果我使用Blob而不是字符串会更快吗?转换是否便宜?

Would it be faster if I was using Blobs instead of strings? Is that conversion cheap?

基本上我需要一种方法,使用JavaScript,将一个非常大的文件写入磁盘,而不是在任何一点将整个内容加载到内存中。我知道你可以给 createObjectURL 一个文件,但这在我的情况下不起作用,因为我正在从一个文件中生成一个文件。用户提供。

Basically I need a way, with JavaScript, to write a really big file to disk without loading the whole thing into memory at any one point. I know that you can give createObjectURL a File, but that doesn't work in my case since I'm generating a new file from one the user provides.

推荐答案

存储Blob会占用更少的空间和资源,因为不再需要转换为base64 。你甚至可以将text / plain对象存储为blob:

Storing a Blob will use a lot less space and resources as there is no longer a need for conversion to base64. You can even store "text/plain" objects as blobs:

var blob = new Blob(['blob object'], {type: 'text/plain'});
var store = db.transaction(['entries'], 'readwrite').objectStore('entries');

// Store the object  
var req = store.put(blob, 'blob');
req.onerror = function(e) {
    console.log(e);
};
req.onsuccess = function(event) {
    console.log('Successfully stored a blob as Blob.');
};

您可以在此处查看更多信息:
https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/

You can see more info here: https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/

自2014年夏天以来,Chrome一直支持此功能: http://updates.html5rocks.com/2014/07/Blob-support-for-IndexedDB-landed-on-Chrome-Dev 所以你不能在较旧版本的Chrome上使用此功能。

Chrome has supported this only since summer of 2014: http://updates.html5rocks.com/2014/07/Blob-support-for-IndexedDB-landed-on-Chrome-Dev so you cannot use this on older versions of Chrome.

这篇关于如何在不崩溃浏览器的情况下使用IndexedDB创建一个非常长的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆