我必须上传一个名称取决于发送的变量的文件到请求。所以我有一个声明的变量imageType
,用于multer.diskStorage
我还有另一个变量用于动态路径,imagePath
它的工作方式与我使用的相同imageType
,但我从undefined-logo.jpg
获得imageType
。这是代码:
var imagePath = '';
var imageType = '';
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, imagePath);
},
filename: function (req, file, callback) {
callback(null, (imageType + '-logo.jpg'));
}
});
var upload = multer({ storage: storage }).single('file');
router.route('/representatives/:id/loginLogo')
.post((req, res, next) => {
/////SOME CODE BEFORE/////
if (rows[0].client_directory == '') {
imagePath = `${rows[0].subdomain_directory}`;
} else {
imagePath = `${rows[0].subdomain_directory}/${rows[0].client_directory}/`;
}
imageType = req.body.imageType;
upload(req, res, function (err) {
if (err) {
return next(err);
}
var sql = `UPDATE representatives SET login_logo = 'jpg' WHERE id = ${req.params.id}`;
conn.query(sql, (err, rows, fields) => {
if (err) {
return next(err);
}
res.json({ message: 'Image uploaded!', representative: req.body.imageType}); //HERE RETURNS req.body.imageType OK
})
})
})
你可以帮帮我吗?谢谢!