【发布时间】:2018-07-03 20:17:29
【问题描述】:
我想通过输入值而不是字段名来自定义我的应用程序的文件名。
换句话说,我想将 file.filename 更改为 req.body.my_field 之类的东西,my_field 是输入的名称。
最后,我想要这样的东西:
"filename": "value_of_my_field-1530606094020.jpg",
"path": "uploads\\value_of_my_field-1530606094020.jpg",
注意到 my_field 的值出现在我的控制台上。
我需要一些帮助才能做到这一点。
这是我的代码:
var express = require('express')
var multer = require('multer')
const path = require('path')
// Init app
var app = express()
const port= 3000
// Set storage engine
const storage = multer.diskStorage({
destination: './uploads/',
filename: function(req, file, cb){
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
// Init upload
const upload = multer({
storage: storage,
limits:{fileSize:1000000},
});
app.post('/upload', upload.single('myImage'), function (req, res, next) {
if(req.file == undefined){
res.json({'Error': 'No File Selected!'});
} else {
res.json(req.file);
console.log(req.body.my_field);
}
})
app.listen(port, () => console.log('server started on port '+ port));
提前致谢
【问题讨论】:
标签: node.js express filenames multer