Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting arraybuffer to string : Maximum call stack size exceeded

This is my code.

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();

That request is being made to a PDF URL which is around 3 MB in size. I have read a few threads with same error, Maximum call stack size exceeded, telling me that there must be some recursive call but I do not see any recursive call here. Can anyone help?

like image 248
Suresh Atta Avatar asked Jul 18 '16 09:07

Suresh Atta


1 Answers

I had the same problem and I finally used 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 );
}
like image 141
Anthony O. Avatar answered Sep 22 '22 23:09

Anthony O.