0
我使用Multer解析在梯形環境多形式,並且不能夠進入我的路由控制器內req.body和req.file數據Multer不填充req.body和req.file
路線/ index.js
var keystone = require('keystone'),
middleware = require('./middleware');
var bodyParser = require('body-parser');
//multi-part form parser for FTP api
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({storage: storage});
exports = module.exports = function(app) {
app.use(bodyParser.json({limit: '10mb'}));
app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
app.post('/upload_api', upload.single('formFile'), routes.api.uploadFTP);
};
路線/ API/uploadFTP.js
var keystone = require('keystone');
var ftpMod = require('ftp');
var fs = require('fs');
exports = module.exports = function(req, res) {
console.log("req.body is ");
console.log(req.body);
console.log("req.file is ");
console.log(req.file);
res.send("console.log() outputted to screen");
}
公共/測試upload.html
<html>
<body>
<form name="sampleForm" enctype="multipart/form-data" action="/upload_api" method="post">
<p>Method</p>
<input type="text" name="method"><br>
<p>Options</p>
<input type="text" name="options"><br>
<p>File</p>
<input type="file" name="formFile"><br><br>
<input type="submit" value="Click to Send">
</form>
</body>
</html>
我來自的NodeJS收到的反應是
>req.body is
{}
req.file is
undefined
我期待req.body
包含{方法:「SomeText」則會} 和req.file
要填充
林困惑除去'upload.single( 'formFile')從index.js '中間件我也對uploadFTP.js文件中的函數upload(req,res,function(err){})感到困惑: upload.single()去哪了? 這一切看起來都與Express/Muller – user258667
提供的文檔大不相同。文檔中的示例代碼是 var multer = require('multer'); var storage = multer.memoryStorage(); var upload = multer({storage:storage}); app.post('/ profile',upload.array(),function(req,res,next){//要求包含文本字段 }); – user258667
這是我的應用程序的工作代碼。如果你願意,你可以保留中間件。我已經從實際的方法 –