我正在尝试上载pdf并将其保存在Express服务器的文件系统中。
当我调用upload.single
时,似乎有一个files
对象,它返回成功,并且什么也没写。
当我调用upload.array
时,没有对象,它返回成功,并且没有任何内容。
const express = require('express')
const router = express.Router()
const db = require('../controllers/upload')
const upload = multer({ dest: 'uploads/' })
router.post('/upload-file', upload.single('pdf'), (req, res, next) => {
console.log(req.file) // logs undefined
console.log(req.files) // logs { file object }
const file = req.files
if (!file) {
const error = new Error('Please upload a file')
error.httpStatusCode = 400
return next(error)
}
res.send(file)
})
router.post('/upload-file-alt', upload.array('pdf', 1), (req, res, next) => {
console.log(req.file) // logs undefined
console.log(req.files) // logs []
const file = req.files
if (!file) {
const error = new Error('Please upload a file')
error.httpStatusCode = 400
return next(error)
}
res.send(file)
})
module.exports = router
如何解析传入的文件并将其保存到磁盘?
注意:文件名是_test.pdf
。