receiving file in node-express uploaded with xhr

喜你入骨 提交于 2020-01-06 20:18:03

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/33669500/receiving-file-in-node-express-uploaded-with-xhr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!