如何使用Koa上传文件?

时间:2019-04-26 17:02:47

标签: node.js koa koa2

我正在设置新服务器,并希望支持高级上传功能。首先,我需要验证文件(文件类型,文件大小,最大数量),最后将其上传到某个目标位置。我用koa-multer尝试了一些操作,但是无法获得multer验证错误。

multer.js

const multer = require('koa-multer')

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/uploads/')
  },
  filename: function (req, file, cb) {
    var fileFormat = (file.originalname).split('.')
    cb(null, file.fieldname + '_' + Date.now() + '.' + fileFormat[fileFormat.length - 1])
  }
})

const fileFilter = (req, file, cb) => {
  if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
    cb(null, true)
  } else {
    cb(new Error('This is not image file type'), false)
  }
}

const upload = multer({
  storage: storage,
  limits: {
    fileSize: 1024 * 1024 * 1,
    files: 5
  },
  fileFilter: fileFilter
})

module.exports = upload

router.js

const Router = require('koa-router')

const multer = require('../middlewares/multer')
const auth = require('../middlewares/auth')
const controller = require('../controllers').userController

const schemas = require('../schemas/joi_schemas')
const validation = require('../middlewares/validation')

const router = new Router()

const BASE_URL = `/users`

router.post(BASE_URL, auth , validation(schemas.uPOST, 'body'), controller.

addUser)
    router.put(`${BASE_URL}/:id`, auth , multer.single('logo')(ctx, (err) => {
  if (err) {
    ctx.body = {
      success: false,
      message: 'This is not image file'
    }
  }
}),  controller.editUser)
router.delete(`${BASE_URL}/:id`, auth , controller.deleteUser)

module.exports = router.routes()

我该如何以最佳方式解决上传问题,以便长期维护代码?

3 个答案:

答案 0 :(得分:1)

koa中间件就像一个嵌套的回调,您应该在加注之后不要捕获“ next()”

router.put(`${BASE_URL}/:id`, auth , async (ctx, next) => {
  try{
    await next()
  } catch(err) {
    ctx.body = {
      success: false,
      message: 'This is not image file'
    }
  }
}, multer.single('logo'),  controller.editUser)

但是您这样做,它也会捕获controller.editUser错误,而控制器本身并没有捕获到错误。

答案 1 :(得分:1)

最简单的文件上传方法如下(假设表单中有一个名为avatar的文件上传字段:

const Koa = require('koa')
const mime = require('mime-types')
const Router = require('koa-router')
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'})

router.post('/register', koaBody, async ctx => {
    try {
        const {path, name, type} = ctx.request.files.avatar
        const fileExtension = mime.extension(type)
        console.log(`path: ${path}`)
        console.log(`filename: ${name}`
        console.log(`type: ${type}`)
        console.log(`fileExtension: ${fileExtension}`)
        await fs.copy(path, `public/avatars/${name}`)
        ctx.redirect('/')
    } catch(err) {
        console.log(`error ${err.message}`)
        await ctx.render('error', {message: err.message})
    }
})

请注意,此示例使用Async Functions,该代码允许使用标准try-catch块清除代码以处理异常。

答案 2 :(得分:0)

您可以使用以下两个选项之一:

第一个方法是在路由末尾添加回调函数。

dom

第二个选项是在调用multer中间件之前添加try / catch:

const multer = require('@koa/multer')

//Options to limit file size and file extension
const upload = multer({
        dest: '../avatars',
        limits: {
            fileSize: 1024*1024
        },
        fileFilter(ctx, file, cb) {
            if (!file.originalname.match(/\.(jpg|jpeg|png)$/)) {
                return cb(new Error('Please upload a World document'))
            }
            cb(undefined, true)
        }
    })



//The last callback should handle the error from multer
router
        .post('/upload', upload.single('upload'), async (ctx) => {
            ctx.status = 200
        }, (error, ctx) => {
            ctx.status = 400
            ctx.body = error.message
        })
})        

在最后一种情况下,如果将异常添加到multer中,它将在上一个wait next()中由try / catch处理