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);
}, ''));
来源
这篇关于将数组缓冲区转换为字符串 - 超出最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将数组缓冲区转换为字符串 - 超出最大调用堆栈
基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
