title: Converting base64 to blob in javascript
body: <p>I tried to convert a JPEG's base64 string to a blob on a Cordova/hybrid app running on iOS 8 using the following function <code>b64toBlob</code>.</p>
  
  <pre><code>b64toBlob = function(b64, onsuccess, onerror) {
      var img = new Image();
  
      img.onerror = onerror;
  
      img.onload = function onload() {
          var canvas = document.createElement('canvas');
          canvas.width = img.width;
          canvas.height = img.height;
  
          var ctx = canvas.getContext('2d');
          ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  
          canvas.toBlob(onsuccess);
      };
  
      img.src = b64;
  }
  </code></pre>
  
  <p>However it's giving an error</p>
  
  <pre><code>Uncaught Error: TypeError: undefined is not a function (evaluating 'canvas.toBlob(onsuccess)')
  </code></pre>
  
  <p>when we do</p>
  
  <pre><code>var imageData = "data:image/jpeg;base64," + imageData
  
  b64toBlob(imageData,
      function(imageBlob) {
         uploadBlob(imageBlob)
      }, function(error) {
          console.log(error)
      });
  </code></pre>
  
  <p>How can be work around this error?</p>