MattA
MattA

Reputation: 255

File Upload not working with Angular and Node.JS

I'm having issues getting file upload to work with the Angular ng-File-Upload module and my Node.js server. In the examples it says that I should be able to access the files that came across in the client request via a "file" or "files" attribute (req.file or req.files).However such attributes upon request are "undefined" and I can find no trace of the files in the client request.

My client side code looks like this:

$scope.addFiles = function(files){
        console.log(files);
        $scope.information.files = files;
            Upload.upload({
                url: 'http://www.uploadURL.com/uploadFiles',
                file: files
            }).progress(function(evt){
                console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
            }).success(function(data, status){
                console.log('data '+ data);
                console.log('status '+status);
            }).error(function(data, status){
                console.log('bad data '+ data);
                console.log('bad status '+ data);
            });
        }
    };

The line of HTML for uploading looks like this:

<button class="btn btn-primary btn-lg btn-block uploadButton" type="file" ngf-select="addFiles($files)" multiple accept="*"> Select Files</button>

and the server side currently looks like this:

app.post('/uploadFiles', function(req, res){
        var tempfiles = [];
        console.log(req.file);
        console.log(req.files);
});

Both of the above console logs return "undefined". If anyone has any idea on how to fix this/ a better method for file upload I would be extremely grateful.

Upvotes: 2

Views: 1146

Answers (1)

Sasikanth Bharadwaj
Sasikanth Bharadwaj

Reputation: 1457

The post data is in the body of the request and you would need the body-parser middleware to parse the post data and set it as fields on the request. Check this out

Upvotes: 2

Related Questions