title: expressjs parse form data post including files and Json
body: <p>I post many files + Json data in one request from Angularjs to expressjs</p>
  
  <p>Angularjs: </p>
  
  <pre><code>function myController($scope, $routeParams, $location, $http) {
      $scope.files = [];
      $scope.$on("fileSelected", function (event, args) {
             $scope.$apply(function () {            
              //add the file object to the scope's files collection
              $scope.files.push(args.file);
          });
          });
  
      $scope.submit = function() {
          $scope.json = {
              phone: this.phone,
              description: this.description,
          };
  
  
          $http({
              method: 'POST',
              url: '/phones/'+ $routeParams.phoneId,
              //IMPORTANT!!! You might think this should be set to 'multipart/form-data' 
              // but this is not true because when we are sending up files the request 
              // needs to include a 'boundary' parameter which identifies the boundary 
              // name between parts in this multi-part request and setting the Content-type 
              // manually will not set this boundary parameter. For whatever reason, 
              // setting the Content-type to 'false' will force the request to automatically
              // populate the headers properly including the boundary parameter.
              // headers: { 'Content-Type': false },
              //This method will allow us to change how the data is sent up to the server
              // for which we'll need to encapsulate the model data in 'FormData'
              transformRequest: function (data) {
                  var formData = new FormData();
                  //need to convert our json object to a string version of json otherwise
                  // the browser will do a 'toString()' on the object which will result 
                  // in the value '[Object object]' on the server.
                  formData.append("model", angular.toJson(data.model));
                  //now add all of the assigned files
                  for (var i = 0; i &lt; data.files; i++) {
                      //add each file to the form data and iteratively name them
                      formData.append("file" + i, data.files[i]);
                  }
                  return formData;
               },
              //Create an object that contains the model and files which will be transformed
              // in the above transformRequest method
              data: { model: $scope.seller_submit, files: $scope.files }
  
          }).
          success(function (data, status, headers, config) {
              alert("success!");
          }).
          error(function (data, status, headers, config) {
              alert("failed!");
          });
  
       };
  };
  </code></pre>
  
  <p>Expressjs 3.x:</p>
  
  <pre><code>app.use(express.bodyParser({ keepExtensions: true, uploadDir: __dirname + '/public/uploads' }));
  </code></pre>
  
  <p>To handle request I just use: </p>
  
  <pre><code>console.log(req.body); //print Json is ok but can not print files info
  console.log(req.files); // can not print files info
   //Both of above handlers generate the same error "Can not call method "replace" of undefined.
  </code></pre>
  
  <p>The problem I think that is express can not parse the multi-file upload request. Please help!</p>