title: Using Node Buffer or fileStream with formData file upload
body: <p><strong>Update</strong>: I have more or less solved the problem using multipart (<code>app.use(multipart({uploadDir: __dirname + '/../uploads'}))</code>from <a href="https://github.com/danialfarid/angular-file-upload/wiki/node.js-example" rel="nofollow noreferrer">these instructions</a>), but still don't know why my original code (below) fails.</p>
  
  <p>There have been numerous variations on this question, and I have tried the ideas there without success. I'm using a <a href="https://github.com/leon/angular-upload" rel="nofollow noreferrer">file uploading directive</a> (and have since tried another open source alternative) to send a binary file to a node server, that runs the following code (based on an SO answer I can't now refind):</p>
  
  <pre><code>exports.receive = function(req, res) {
  
      var fitFileBuffer = new Buffer('');
      // req.setEncoding("binary");    //doesn't help
  
      req.on('data', function(chunk) { 
          fitFileBuffer = Buffer.concat([fitFileBuffer, chunk]);
      });
  
      req.on('end', function() { 
          fs.writeFileSync(
              "today2.fit",
              fitFileBuffer,
              'binary');
          res.send(200);
      });
  };
  </code></pre>
  
  <p>If I upload <code>today.fit</code> and compare to <code>today2.fit</code>, they have the same Kb of data, but are not identical, and subsequent code fails to process the file. Given that this happens with two pieces of third party code I suspect the problem lies with my code.</p>
  
  <p>Here are the details from the client side of the POST being made
  <img src="https://i.stack.imgur.com/aYwhS.png" alt="Network settings"> </p>