title: Upload file via AngularJS and NodeJS,
body: <p>I am now using Express4 and want to upload files to the file system of the server. (for example, save the files in the <code>public/files</code> folder.)</p>
  
  <p>There are several questions about how to user <code>formidable</code> or <code>busboy</code> to parse the request like this example:</p>
  
  <pre><code>var formidable = require('formidable'),
      http = require('http'),
      util = require('util');
  
  http.createServer(function(req, res) {
    if (req.url == '/upload' &amp;&amp; req.method.toLowerCase() == 'post') {
      // parse a file upload 
      var form = new formidable.IncomingForm();
  
      form.parse(req, function(err, fields, files) {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write('received upload:\n\n');
        res.end(util.inspect({fields: fields, files: files}));
      });
  
      return;
    }
  
    // show a file upload form 
    res.writeHead(200, {'content-type': 'text/html'});
    res.end(
      '&lt;form action="/upload" enctype="multipart/form-data" method="post"&gt;'+
      '&lt;input type="text" name="title"&gt;&lt;br&gt;'+
      '&lt;input type="file" name="upload" multiple="multiple"&gt;&lt;br&gt;'+
      '&lt;input type="submit" value="Upload"&gt;'+
      '&lt;/form&gt;'
    );
  }).listen(8080);
  </code></pre>
  
  <p>Such middleware could parse <code>req</code> to get the metadata, like the file path, name, size, etc. However, if I want to POST different files to different endpoints, I would like to use angularjs controller and service structure to process the uploading. </p>
  
  <p>Wondering how can I get file path, name, etc to controller/service and post them to the backend. Then I can use the following codes to upload files.</p>
  
  <pre><code>api.post(url, function(req, res) {
    var fs = require('fs');
    //req.body.path: path of file, like, C:\\user\John\.....
    //req.body.filename: name of file, like images.jpg
    fs.readFile(req.body.path, function(err, data){
      var uploadToPath = './public/files/' + req.body.filename;
  
      fs.writeFile(uploadToPath, data, function(err){
        if(err) {
          res.send(err);
          return;
        }
      });
    });
  });
  </code></pre>
  
  <p>I am also referring <a href="http://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs" rel="nofollow">this solution</a>, but it can only get the info like file size, but not the very important file path. </p>
  
  <p>pls advise, many thanks.</p>