Converting array buffer to string - Maximum call stack size exceeded(将数组缓冲区转换为字符串 - 超出最大调用堆栈大小)
问题描述
我们的应用下载了一个 zip 文件,但响应是二进制的.
Our app downloads a zip file, but the response is in binary.
所以我所做的就是将其转换为 base64.大小为87.7KB时有效,但响应大小为183KB时出错.
So what I did is to convert it to base64. It works when the size is 87.7KB but an error occurs when the response size is 183KB.
错误是Uncaught RangeError: Maximum call stack size exceeded
有问题的行是
btoa(String.fromCharCode.apply(null, new Uint8Array(blob)))
根据this answer,必须替换 String.fromCharCode.apply()使用 TextEncoder.
According to this answer, the String.fromCharCode.apply() must be replaced with TextEncoder.
所以我改成
btoa(new TextDecoder('utf-8').decode(new Uint8Array(blob)))
但我得到一个错误.
Uncaught DOMException: Failed to execute 'btoa' on 'Window': 要编码的字符串包含 Latin1 范围之外的字符.
我使用此answer
现在是新代码
btoa(unescape(encodeURIComponent(new TextDecoder('utf-8').decode(new Uint8Array(blob)))))
现在可以下载,但下载的 zip 文件已损坏.
The download now works but the download zip file is corrupted.
整个代码可见这里
推荐答案
我从另一个问题得到了答案
I got my answer from another question
btoa(new Uint8Array(blob).reduce(function (data, byte) {
return data + String.fromCharCode(byte);
}, ''));
来源
这篇关于将数组缓冲区转换为字符串 - 超出最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将数组缓冲区转换为字符串 - 超出最大调用堆栈
基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
