Search code examples
javascriptbinaryinternet-explorer-9base64polyfills

IE9 - DataURI to Binary - Polyfills for .atob, Uint8Array and ArrayBuffer - Array too large for polyfill


I have the following utility:

        // Utility function to remove base64 URL prefix and store base64-encoded string in a    Uint8Array
        // Courtesy: https://gist.github.com/borismus/1032746
        function convertDataURIToBinary(dataURI) {
            var BASE64_MARKER = ';base64,';
            var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
            var base64 = dataURI.substring(base64Index);
            var raw = window.atob(base64);
            var rawLength = raw.length;
            var array = new Uint8Array(new ArrayBuffer(rawLength));

            for (i = 0; i < rawLength; i++) {
                array[i] = raw.charCodeAt(i);
            }
            return array;
        }

which I use like this:

        // Read the binary contents of the base 64 data URL into a Uint8Array
        // Append the contents of this array to the SP.FileCreationInformation
        var arr = convertDataURIToBinary(convertedDataURI);
        for (var i = 0; i < arr.length; ++i) {
            fileCreateInfo.get_content().append(arr[i]);
        }

To prepare a fileupload.

To make it work in IE9 I used following polyfills:

Now I get a quite useful exception while creating the Uint8Array:

SCRIPT5022: Exception thrown and not caught

After some debugging I found the problem:

if (obj.length > MAX_ARRAY_LENGTH) throw RangeError('Array too large for polyfill');

My object has a length of 1085798 and MAX_ARRAY_LENGTH equals 100000. I could change this value but I guess theres a reason for it.

Anyone knowing a better way to do this?


Solution

  • Well I got the answer.

    I just set

    MAX_ARRAY_LENGTH = 2000000; //value that is higher than the expected array.length (may I should just remove the check)
    

    the result is, that the browser freezes due to the big array but after a few (maybe some more) seconds it unfreezes and the upload is done.

    The MAX_ARRAY_LENGTH is used to maintain some speed while using it.