title: IE9 - DataURI to Binary - Polyfills for .atob, Uint8Array and ArrayBuffer - Array too large for polyfill
body: <p>I have the following utility:</p>
  
  <pre><code>        // 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 &lt; rawLength; i++) {
                  array[i] = raw.charCodeAt(i);
              }
              return array;
          }
  </code></pre>
  
  <p>which I use like this:</p>
  
  <pre><code>        // 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 &lt; arr.length; ++i) {
              fileCreateInfo.get_content().append(arr[i]);
          }
  </code></pre>
  
  <p>To prepare a fileupload. </p>
  
  <p>To make it work in IE9 I used following polyfills: </p>
  
  <ul>
  <li><a href="https://code.google.com/p/stringencoders/source/browse/trunk/javascript/base64.js?r=230" rel="nofollow">base64</a> -- .atob</li>
  <li><a href="https://github.com/inexorabletash/polyfill/blob/master/typedarray.js" rel="nofollow">TypedArray</a> -- UInt8Array / ArrayBuffer</li>
  </ul>
  
  <p>Now I get a quite useful exception while creating the Uint8Array:</p>
  
  <blockquote>
    <p>SCRIPT5022: Exception thrown and not caught</p>
  </blockquote>
  
  <p>After some debugging I found the problem: </p>
  
  <pre><code>if (obj.length &gt; MAX_ARRAY_LENGTH) throw RangeError('Array too large for polyfill');
  </code></pre>
  
  <p>My object has a <code>length</code> of <code>1085798</code> and <code>MAX_ARRAY_LENGTH</code> equals <code>100000</code>. I could change this value but I guess theres a reason for it.</p>
  
  <p><strong>Anyone knowing a better way to do this?</strong></p>