2

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

So via body-parser, what might the code look like for parsing the body in this particular situation? It seems as though there are many options to choose from (url-encoded, raw, text, Json).
There's an example here github.com/danialfarid/ng-file-upload/wiki/Node-example that shows how to use ng-file-upload with another middleware. Check it out
I ended up using a different parser than recommended however it works! Thanks for the help/direction

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.