将arraybuffer转换为字符串:超出最大调用堆栈大小

时间:2016-07-18 09:04:52

标签: javascript jquery encoding data-conversion arraybuffer

这是我的代码行。

        var xhr = new XMLHttpRequest();
        xhr.open('GET',window.location.href, true);
        xhr.responseType = "arraybuffer";
        xhr.onload = function(event) {
         debugger;
         console.log(" coverting array buffer to string "); 
         alert(String.fromCharCode.apply(null, new Uint8Array(this.response)));
        };
        xhr.send();

该请求正在制作一个大约3 MB的pdf网址。阅读几个相同错误的线程,告诉必须有一些递归调用,但我没有看到任何递归调用。有什么帮助吗?

3 个答案:

答案 0 :(得分:9)

错误是由函数参数数量的限制引起的。见"RangeError: Maximum call stack size exceeded" Why?

而不是String.fromCharCode.apply(),请使用e。 G。一个TextEncoder。见Uint8Array to string in Javascript

答案 1 :(得分:4)

我遇到了同样的问题,最后我使用了this code

function _arrayBufferToBase64( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return window.btoa( binary );
}

答案 2 :(得分:1)

---
- hosts: localhost
  gather_facts: no
  vars:
    data: '[
      "just_dir",
      {
         "path": "extend_dir",
         "order": "nginx"
      }
    ]'
  tasks:
   - name: debug just_dir
     debug: msg="{{ data | from_json  | json_query(jmesquery) }}"
     vars:
       jmesquery: "[?type(@) == `string`].{path: @}"

   - name: debug Other data
     debug: msg="{{ data | from_json  | json_query(jmesquery) }}"
     vars:
       jmesquery: "[?type(@) == `object`]"