2017-06-21 163 views
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要填充

回答

0

在你不能得到的值或者req.bodyreq.file。用下面給出的代碼替換youe uploadFTP.js。

var multer = require('multer'); 
var path = require('path'); 

var storage = multer.diskStorage({ 
    destination: function(req, file, callback) { 
    callback(null, <<path where you want to store your files>>); 
    }, 
    filename: function(req, file, callback) { 
    callback(null, path.basename(Date.now() + '_' + file.originalname)); 
    }, 
}); 
var maxSize = 1024 * 1024; 
var upload = multer({ 
    storage: storage, 
    fileFilter: function(req, file, callback) { 
    var ext = path.extname(file.originalname); 
    if(ext !== '.gif' && ext !== '.jpeg') { 
     return callback(res.status(202).end();, null); 
    } 
    callback(null, true); 
    }, 
    limits: {fileSize: maxSize}, 
}).single('image'); 

exports = module.exports = function (req, res) { 

    upload(req, res, function(err) { 
    console.log(storage.diskStorage); 
    if(err) { 
     return res.status(201).end(); 
    } 
    //Do stuff here 
    res.status(200).end(); 
    }); 
} 

替換下面線在index.js

app.post('/upload_api', upload.single('formFile'), routes.api.uploadFTP); 

app.post('/upload_api', routes.api.uploadFTP); 
+0

林困惑除去'upload.single( 'formFile')從index.js '中間件我也對uploadFTP.js文件中的函數upload(req,res,function(err){})感到困惑: upload.single()去哪了? 這一切看起來都與Express/Muller – user258667

+0

提供的文檔大不相同。文檔中的示例代碼是 var multer = require('multer'); var storage = multer.memoryStorage(); var upload = multer({storage:storage}); app.post('/ profile',upload.array(),function(req,res,next){//要求包含文本字段 }); – user258667

+0

這是我的應用程序的工作代碼。如果你願意,你可以保留中間件。我已經從實際的方法 –