title: NodeJS base64 image encoding/decoding not quite working
body: <p>I've been trying to handle saving images POSTed to nodeJS (and the express framework) to a database, and have been having some trouble.  Ignoring all the web processing, I think that I've narrowed down the problem to the way base64 encoding is happening in node. I believe that the oversimplified example below should work, but the output image is always corrupted.   </p>
  
  <p>The example (1) loads in an image (2) saves a copy of if (<code>image_orig</code>) to confirm that node can read the file properly.  This always works.  (3) I take the image and base64 encode its contents, (4) then decode it.  The final output image (<code>image_decoded</code>) is always corrupted.</p>
  
  <p>Help!
  (node.js 0.6.0 on OSX Lion)</p>
  
  <pre><code>console.log("starting");
  process.chdir(__dirname);
  
  var fs = require("fs");
  
  var image_origial = "image.jpg";
  fs.readFile(image_origial, function(err, original_data){
      fs.writeFile('image_orig.jpg', original_data, function(err) {});
      var base64Image = new Buffer(original_data, 'binary').toString('base64');
      var decodedImage = new Buffer(base64Image, 'base64').toString('binary');
      fs.writeFile('image_decoded.jpg', decodedImage, function(err) {});
  });
  </code></pre>