Multer not populating req.files while using postman
我正在研究一个宁静的api,它将读取excel工作表并以json格式返回工作表中的数据。但是当从邮递员上传文件(.xls)并将请求发送到api时。 api崩溃了。
下面是我的代码:
来自app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var app= express(); var upload = multer({ fileFilter : function(req, file, callback) { if (['xls', 'xlsx'].indexOf(file.originalname.split('.')[file.originalname.split('.').length-1]) === -1) { return callback(new Error('Wrong extension type')); } callback(null, true); } }).single('file'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); var routes = require("./routes/routes.js")(app,upload); var server = app.listen(3000, function () { console.log("Listening on port %s...", server.address().port); }); |
routes.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | app.post('/uploadxl', function(req, res) { console.log("upload file"); var exceltojson = require("xls-to-json-lc"); upload(req,res,function(err){ console.log("e"); if(err){ res.json({error_code:1,err_desc:err}); return; } /** Multer gives us file info in req.file object */ if(!req.file){ res.json({error_code:1,err_desc:"No file passed"}); return; } //start convert process /** Check the extension of the incoming file and * use the appropriate module */ if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){ exceltojson = xlsxtojson; } else { exceltojson = xlstojson; } var node_xj = require("xls-to-json"); node_xj({ input: req.file.path, // input xls output:"output.json", // output json sheet:"a" // specific sheetname }, function(err, result) { if(err) { console.log("error"); console.error(err); } else { console.log("--------result----"); res.json(result); } }); }); }); } |
我在Windows命令提示符下收到此错误:
上传文件
?
您错过了输入文件
这显然意味着它正在进入multer的误差函数。
您的问题似乎与此问题相似。
如果您使用的是Postman,则可以尝试删除标题:" Content-type":" multipart / form-data"。