Bala Sivagnanam
Bala Sivagnanam

Reputation: 863

receiving file in node-express uploaded with xhr

I have a xmlhttprequest which uploads the file and I am trying to receive it in my node-express server. but for some reason I am not able to retrieve the file content in the server. Not sure where I am missing it.

app.post('/api/uploadfiles', function(req, res) {
   console.log("apicalled");
   console.log(req);
   console.log(req.body);
   console.log(req.files);
   console.log(JSON.stringify(req.files));
});

enter image description here

Upvotes: 0

Views: 1043

Answers (1)

gibson
gibson

Reputation: 1086

In order for you to see the files, you will need to add another middleware that parses multi-part request. Try using connect-multiparty module like so:

var multipart = require('connect-multiparty'); //for files upload
var multipartMiddleware = multipart();//for files upload

app.post('/api/uploadfiles', multipartMiddleware, function(req, res) {
   console.log("apicalled");
   console.log(req);
   console.log(req.body);
   console.log(req.files);
   console.log(JSON.stringify(req.files));
});

Upvotes: 2

Related Questions