我正在上传文件并正确保存。我目前的问题是我需要文件名包含用户ID或用户名,因此我可以为每个用户分别保存文件。继续我目前的设置
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'myuploadpath')
},
filename: function (req, file, cb) {
console.log(req);
cb(null, + file.originalname)
}
})
var upload = multer({ storage: storage });
这是api电话
router.post('/signup', upload.single("resume"), function(req, res) {
console.log("file:" + req.file);
var user = {
username: req.body.username,
email: req.body.email,
password: req.body.password,
f_name: req.body.f_name,
l_name: req.body.l_name,
city: req.body.city,
state: req.body.state,
zip: req.body.zip
};
connection.query('INSERT INTO user SET ?', user, function(err, result) {
if (err)
throw err;
res.redirect('/login');
});
});
我也在这里创建一个用户,但我也会有一个个人资料页面,他们可以添加一个不同的文件。
我试图在文件名函数中调用req.body.username但它返回“NaN”。有关如何在命名文件之前获取用户名或ID的任何提示吗?