javascript 当数据 uri 太大时“啊,快”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16761927/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:53:26  来源:igfitidea点击:

"Aw, Snap" when data uri is too large

javascriptgoogle-chrome-extensiondata-uri

提问by amccormack

I'm writing a chrome extension which does the following:

我正在编写一个 chrome 扩展程序,它执行以下操作:

  1. Downloads a file from a website to memory using XMLHttpRequest
  2. Adds additional data to the file and then base64 encodes the result to the variable total_encoded_data
  3. Offers the data to the user using <a href=data:application/octet-stream;charset=utf-8;base64,' + total_encoded_data+' download='file.bin'>Click to Download</a>. Where total_encoded_datais added to href using jQuery.
  1. 使用以下命令将文件从网站下载到内存 XMLHttpRequest
  2. 向文件中添加额外的数据,然后将结果以 base64 编码到变量中 total_encoded_data
  3. 使用 向用户提供数据<a href=data:application/octet-stream;charset=utf-8;base64,' + total_encoded_data+' download='file.bin'>Click to Download</a>total_encoded_data使用 jQuery 将Where添加到 href 中。

I have found, through a manual binary search, that if the size of total_encoded_datais greater than 2097100 characters, then I will get an Aw, Snap message when I click the link. If the size is smaller, then I can download as expected.

我通过手动二进制搜索发现,如果 的大小total_encoded_data大于 2097100 个字符,那么当我单击链接时,我会收到一条 Aw, Snap 消息。如果大小较小,那么我可以按预期下载。

In addition to testing the filesize, I also used atoito ensure that the base64 encoding is valid, and it operates without error.

除了测试文件大小之外,我还用来atoi确保base64编码有效,并且运行无误。

The Aw, Snap messages don't produce any crash reports in chrome://crashesnor any unexpected output in the chrome_debug.log

Aw, Snap 消息不会产生任何崩溃报告,chrome://crashes也不会产生任何意外输出chrome_debug.log

How do I avoid an Aw, Snap message when serving a data uri where the base64 encoded string length is greater than 2097100?

在提供 base64 编码字符串长度大于 2097100 的数据 uri 时,如何避免出现 Aw, Snap 消息?

回答by rhashimoto

It's a known chromium bug. The recommended workaround is to use a blob URL. Also see Creating a Blob from a base64 string in JavaScript.

这是一个已知的铬错误。推荐的解决方法是使用blob URL。另请参阅在 JavaScript 中从 base64 字符串创建 Blob