我试图上传两个文件与不同的文件扩展名与multer从两个字段,但当我尝试与 Postman 的结果总是为文件是空,什么是我的问题的解决方案?这里是我的代码
中间件/上传Epub
const multer = require('multer')
exports.uploadEpub = (epubFile, coverFile) => {
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads")
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname.replace(/\s/g, ""))
}
})
const upload = multer({
storage
}).fields([{name: "bookFile", maxCount: 1},{name: "coverFile", maxCount: 1}])
}
主计长/账簿
exports.addBook = async (req, res) => {
try {
const { ...data } = req.body
const newBook = await book.create({
...data,
bookFile: req.file,
coverFile: req.file
})
let bookData = await book.findOne({
where: {
id: newBook.id
},
attributes:{
exclude: ['createdAt','updatedAt']
}
})
bookData = JSON.parse(JSON.stringify(bookData))
res.send({
status: "Success",
Book: {
...bookData
}
})
} catch (error) {
console.log(error)
res.status(500).send({
status: "Failed",
message: "Server Error"
})
}
}
3条答案
按热度按时间dgiusagp1#
Multer设置
/中途岛. js/
/index.js或app.js/ //定义路由的位置
/控制器/书籍/
ruarlubt2#
尝试在postman中将文件作为form-data上传,并将与您在multer中设置的名称相同的密钥放入,postman将如下所示:
fjnneemd3#
"试试这个"
const cpUpload =上载.字段([{名称:“化身”,最大计数:1 },{姓名:'库',最大计数:8 }])
app.post('/酷配置文件',cpUpload,函数(请求,资源,下一个){ })