将数组缓冲区转换为字符串 - 超出最大调用堆栈大小 [英] Converting array buffer to string - Maximum call stack size exceeded

查看:21
本文介绍了将数组缓冲区转换为字符串 - 超出最大调用堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用下载了一个 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);
}, ''));

来源

这篇关于将数组缓冲区转换为字符串 - 超出最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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