receiving file in node-express uploaded with xhr

时间:2015-11-12 10:37:25

标签: node.js express xmlhttprequest

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

1 个答案:

答案 0 :(得分:2)

为了查看文件,您需要添加另一个解析多部分请求的中间件。 尝试使用connect-multiparty模块,如下所示:

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));
});