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);
}, ''));
来源
这篇关于将数组缓冲区转换为字符串 - 超出最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将数组缓冲区转换为字符串 - 超出最大调用堆栈


基础教程推荐
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01