title: base64 encoded image is corrupted when saved to disk
body: <p>I am posting the base64 clipboard image to server (node) and I am saving the base64 to disk. For some reason the image is corrupted.</p>
  
  <p>Client side logic of posting:</p>
  
  <pre><code>function sendData($http, clipboardImage) {
      // $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
      // var imgData = JSON.stringify(clipboardImage);
      //var data = {"imgdata" : clipboardImage}; 
      var url = "http://localhost:3000/pad/img/";
  
      $http({
          method: 'POST',
          url: url,
          data: "data=" + clipboardImage
      });
  }
  
  $("[ng-model='html']").delegate("p", "paste", function(event) {
      var items = (event.clipboardData || event.originalEvent.clipboardData).items;
      console.log(JSON.stringify(items)); // will give you the mime types
      // find pasted image among pasted items
      var blob = null;
      for (var i = 0; i &lt; items.length; i++) {
          if (items[i].type.indexOf("image") === 0) {
              blob = items[i].getAsFile();
          }
      }
      // load image if there is a pasted image
      if (blob !== null) {
          var reader = new FileReader();
          reader.onload = function(e) {
              sendData($http, e.target.result);
          };
          reader.readAsDataURL(blob);
      }
  });
  </code></pre>
  
  <p>Server logic:</p>
  
  <pre><code>app.post("/pad/img/", function(req, res) {
      var imgB64Data = req.body.data;
      var decodedImg = decodeBase64Image(imgB64Data);
      var imageBuffer = decodedImg.data;
      var type = decodedImg.type;
      var extension = mime.extension(type);
      var fileName = "image." + extension;
      try {
          fs.writeFile(fileName, imageBuffer, function(err) {
              console.log(err);
          });
      } catch (err) {
          console.error(err);
      }
  });
  
  function decodeBase64Image(dataString) {
      var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
          response = {};
  
      if (matches.length !== 3) {
          return new Error('Invalid input string');
      }
  
      response.type = matches[1];
      response.data = new Buffer(matches[2], 'base64');
  
      return response;
  }
  </code></pre>
  
  <p>The image is being saved successfully but it seems to be corrupted. Can you please point out what may be missing?</p>