Как загрузить файл с Multer и express - PullRequest
0 голосов
/ 27 апреля 2020

Я пытаюсь загрузить файл 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.

...