1

I am tring to upload a file using angular $http method to node backend

I want to upload the form with additional form fields.

This is my code

        var data = new FormData();
        data.append('title', 'sometitle');
        data.append('uploadedFile', $scope.uploadedFile);
        FileService.upload(url, data, function(data, status) {
          if(status===HTTP_OK) {
            $scope.uploadSuccess = true;
            $scope.showUploadProgressBar = false;
          } else {
            // error occured
            console.log(data);
          }
        });

FileService

FileService.upload = function(url, data, callback) {
    $http({
        method : 'POST',
        url : url,
        data : data,
  headers: {'Content-Type': undefined },
  transformRequest: angular.identity
    }).success(function(data, status) {
        callback(data, callback);
    }).error(function(data, status) {
        callback(data, status);
    });
};

I am using node multiparty module for file upload. I am receiving the file correctly. But the field value for title is undefined.

I don't know why title value is undefined

Node.js backend file upload handler

var form;
if(options.uploads.tempDir) {
  form = new multiparty.Form({uploadDir : options.root + '/' + options.uploads.tempDir});
} else {
  form = new multiparty.Form();
}
form.on('file', function(name, receivedFile) {
  var tmpPath = receivedFile.path,
    fileName = receivedFile.originalFilename,
    targetDirectory = uploadDirectory + '/' + req.params.id,
    targetPath = targetDirectory + '/' + fileName,
    file = {
      filePath : targetPath,
      tempPath : tmpPath,
      fileName : fileName,
      size : receivedFile.size
    };

  fileUploadStatus.file = file;
  // move file
  fse.move(tmpPath, targetPath, function(err) {
    if(err) {
      console.log('Error moving file [ ' + targetPath + ' ] ' + JSON.stringify(err));
    }
  });
});

form.on('error', function(err) {
  fileUploadStatus.err = err;
  req.fileUploadStatus = fileUploadStatus;
  next();
});

form.on('close', function() {
  req.fileUploadStatus = fileUploadStatus;
  next();
});
form.on('field', function(name, value) {
  console.log('field called');
  console.log(name);
  console.log(value);
  req.body = req.body || {};
  req.body[name] = value;
});

// ignoring parts. Implement any other logic here
form.on('part', function(part) {
  var out = new stream.Writable();
  out._write = function (chunk, encoding, done) {
    done(); // Don't do anything with the data
  };
  part.pipe(out);
});

// parsing form
form.parse(req);
5
  • Why are you setting Content-Type to undefined and can you post the code where you're using multiparty on the backend? Commented Sep 1, 2014 at 13:27
  • @mscdex check the edit. I am setting content-type undefined because I read somewhere that If I set it as undefined it will auto compute content type and boundary Commented Sep 1, 2014 at 13:31
  • So field called is not logged to the console? Commented Sep 1, 2014 at 13:33
  • Field called is logged to console but the value is empty string Commented Sep 1, 2014 at 13:34
  • @mscdex Can you able to identify the problem Commented Sep 1, 2014 at 14:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.