Our app downloads a zip file, but the response is in binary.
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
.
The error is Uncaught RangeError: Maximum call stack size exceeded
The line in question is
btoa(String.fromCharCode.apply(null, new Uint8Array(blob)))
According to this answer, the String.fromCharCode.apply()
must be replaced with TextEncoder
.
So I changed it to
btoa(new TextDecoder('utf-8').decode(new Uint8Array(blob)))
but I get an error.
Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
I changed it again using the top most snippet of this answer
The new code is now
btoa(unescape(encodeURIComponent(new TextDecoder('utf-8').decode(new Uint8Array(blob)))))
The download now works but the download zip file is corrupted.
The whole code can be seen here
The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.
The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case.
It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit. This is almost always because of a recursive function with a base case that isn't being met.
Without any local variables, each function call takes up 48 bytes during the execution, and you are limited to less than 1MB for all local function frames. Each boolean and number variable takes 8 bytes of memory.
I got my answer from another question
btoa(new Uint8Array(blob).reduce(function (data, byte) {
return data + String.fromCharCode(byte);
}, ''));
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With