22.nodejs 中 koa框架上传文件 koa-multer

const multer = require('koa-multer')

const storage = multer.diskStorage({
  destination: function (ctx, file, cb) {
    cb(null, 'upload/files')
  },
  filename: function (ctx, file, cb) {
    const fileName = (file.originalname).split('.')[0].toString('binary')
    const fileFormat = (file.originalname).split('.')[1]
    cb(null, (fileName + '-' + Date.now() + '.' + fileFormat)) //decodeURI()
  }
})

const limits = {
  fileSize: 1024 * 1024,
  files: 1
}

const upLoad = multer({
  storage,
  limits
}).single('file')

module.exports = {
  upLoad
}
const router = require('koa-router')()
const { downLoad } = require('../middlerwares/download')
const { upLoad } = require('../middlerwares/upload')
const multer = require('koa-multer')

router.post('/upload', upLoad, async (ctx, next) => {
  await ctx.render('main', {
    // bbb: decodeURI(ctx.req.file.filename),
    bbb: ctx.req.file.filename,
    aaa: `http://127.0.0.1:3000/files/${ctx.req.file.filename}`
  })
})

router.get('/download', downLoad, async (ctx, next) => {

})

module.exports = router
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页