【发布时间】:2018-03-29 21:54:53
【问题描述】:
我想在我的快速路由块中使用 multer,即不作为中间件。我拥有的 multer 配置可用作中间件,但我想在调用 multer 之前进行几次检查。
所以我尝试了这个,但无济于事:
/*
* Upload a file
*/
const MediaPath = "/var/tmp";
var multer = require('multer'); // Multer is for file uploading
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, MediaPath + '/' + req.params.organisation + '/' + req.params.table + '/');
},
filename: function (req, file, cb) {
// TODO - remove the time and timezone from ISO string, resolve the correct filename, create thumbnail,
var date = new Date();
cb(null, req.params.id + '.' + date.toISOString());
}
})
//var upload = multer({ storage: storage });
var upload = multer({ storage: storage }).single('file');
router.post('/file/:organisation/:table/:id', function (req, res, next){
db.resolveTableName( req )
.then( table => {
auth.authMethodTable( req )
.then( function() {
console.log('Uploading a file: ');
upload(req, res, function( err ) {
if( err ) {
console.log('Upload error' );
res.status(500).json( err );
}
console.log('Upload success' );
res.status(200).json("success");
});
})
.catch( function( error ) {
res.status(401).json('Unauthorized');
});
})
.catch( function(e) {
res.status(400).json('Bad request');
});
});
有趣的是我没有收到错误,所以返回 200,但我没有收到上传的文件。
有什么想法吗?
【问题讨论】:
标签: express file-upload multer