gdubs gdubs - 1 year ago 128
Node.js Question

node uploading file $ from angularjs has undefined req.body

I'm building a file upload functionality with my angularjs app that would upload a file to my node api that will ftp to a cdn server. Right now I'm stuck with just getting hte file. I tried using multer but I'm not sure how to prevent the save to redirect to an ftp.

Anyway, this is my code withoout multer

<input type="file" multiple file-model="fileRepo"/>

myApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('change', function(){


$scope.saveFile = function(){
var fd=new FormData();

$scope.newFile.files = fd;


/// fileservice ///

uploadFile: function(file){
var deferred = $q.defer();

var uploadUrl = '/api/file/ftp/new';
var requestFileUpload = {
method: 'POST',
url: uploadUrl,
data: file.files
var requestFileUploadConfig = {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
$, file.files, requestFileUploadConfig)

/// node part ///'/ftp/new', function(req, res) {
console.log('file is ' + JSON.stringify(req.body));

Answer Source

You'll have to use an HTML parser you are not going to be able to catch the file just by reading the request.

I'd recommend use busboy and connect-busboy then you are going to be able to read your file, this a small example:

req.busboy.on('file',function(fieldname, file, filename, encoding, contentType){
    // get data 

    }).on('end', function(){


req.busboy.on('field',function(fieldname, val){
    req.body[fieldname] = val;

req.busboy.on('finish', function() {
    // save file here