作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我不知道如何使用 req.body.fname 作为文件名,甚至尝试使用中间件,但 req.body 是空的。
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path);
},
filename: function (req, file, cb) {
cb(null, req.body.fname) // undefined
}
})
var upload = multer({ storage: storage })
app.get('/upload', upload.single('fname'), (req,res)=>{
.......
})
i m unable to figure out how to fetch fname in fileName
index.html
<form action="/upload" method="POST" enctype= "multipart/form-data">
<input type="text" name="fname">
<input type="file" name="pic">
<input type = "submit">
</form>
最佳答案
这不是一种优雅的方式,但总比没有好。
app.post('/upload', (req, res) => {
upload(req, res, function (err) {
console.log(req.body.fname) // Here it works
});
});
const fs = require('fs');
然后我们回到上传过程。
app.post('/upload', (req, res) => {
upload(req, res, function (err) {
fs.renameSync(req.files.path, req.files.path.replace('undefined', req.body.fname));
// This get the file and replace "undefined" with the req.body field.
});
});
我假设您的文件路径没有名为“undefined”的文件夹。在这种不太可能发生的情况下,只需将文件命名为 Multer 并稍后将其替换为 fs.renameSync。
const path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path);
},
filename: function (req, file, cb) {
cb(null, req.body.fname + path.extname(file.originalname))
}
})
或者在不太可能的情况下,您需要“.undefined”扩展名,只需稍后在 fs 重命名过程中附加扩展名即可。
希望这能解决您的问题。编码愉快!
关于node.js - 多自定义文件名 req.body.inputTextField 作为文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52131922/
我是 Swift 的新手,我正在尝试更改 Start Developing iOS Apps (Swift)如何更好地理解它。我用这个 Main.storyboard 创建了一个简单的单 View 应
我不知道如何使用 req.body.fname 作为文件名,甚至尝试使用中间件,但 req.body 是空的。 var storage = multer.diskStorage({ destina
我是一名优秀的程序员,十分优秀!