String.fromCharCode.apply Maximum call stack size exceeded

在pc端,下载后台返回base64格式,文件过大时候
String.fromCharCode.apply(null, new Uint16Array(data))
报内存越界 Maximum call stack size exceeded

报错原因:
使用这个处理报内存越界 String.fromCharCode.apply(null, new Uint16Array(data));
解决:

return String.fromCharCode.apply(null, array);
替换
var res = '';
var chunk = 8 * 1024;
var i;
for (i = 0; i < array.length / chunk; i++) {
  res += String.fromCharCode.apply(null, array.slice(i * chunk, (i + 1) * chunk));
}
res += String.fromCharCode.apply(null, array.slice(i * chunk));
return res;

没有报错前代码

  var strData = atob(res.data);
    // Convert binary string to character-number array
    var charData = strData.split('').map(function (x) { return x.charCodeAt(0); });
    // Turn number array into byte-array
    var binData = new Uint8Array(charData);
    // // unzip
    var data = pako.inflate(binData);
    // Convert gunzipped byteArray back to ascii string:
    strData = String.fromCharCode.apply(null, new Uint16Array(data));
    // 将乱码的中文进行转换
    let jsonResult = decodeURIComponent(escape((strData)))

处理后代码:

 let strData = atob(result.data);
        // Convert binary string to character-number array
        let charData = strData.split('').map(function (x) { return x.charCodeAt(0); });
        // Turn number array into byte-array
        let binData = new Uint8Array(charData);
        // // unzip
        let array = pako.inflate(binData);

        strData = ''
        /**
         * String.fromCharCode.apply(null, array) 显示 Maximum call stack size exceeded
         * 超过最大调用堆栈大小
         * 
         */
        let chunk = 8 * 1024;
        let i;
        for (i = 0; i < array.length / chunk; i++) {
          strData += String.fromCharCode.apply(null, array.slice(i * chunk, (i + 1) * chunk));
        }
        strData += String.fromCharCode.apply(null, array.slice(i * chunk));

        // 将乱码的中文进行转换
        let jsonResult = decodeURIComponent(escape((strData)))

下载文章:https://segmentfault.com/a/11...

参考文章:
原文链接:https://blog.csdn.net/woaimum...

你可能感兴趣的:(javascript,vue.js,前端,html)