首页 > 解决方案 > 在 express js 中使用路由器进行 Multer 存储

问题描述

我有这条路线,当我把desk一切都很好的时候,我可以上传我的图片,但是当我使用storage对象时它不起作用并且找不到路线,如果我使用storage一切app.use正常的话。

我可以通过使用来处理这个问题,app.use但我是快递新手,很好奇为什么它不起作用!!!

谢谢你的帮助 :)

router.route("/").post(
            multer({
                //dest: 'uploads/expense'
                storage: multer.diskStorage({
                    destination: (req, file, next) => {
                      next(null, path.join(__dirname, '/uploads/expense'))
                    },
                    filename: (req, file, next) => {
                        next(null, file.fieldname +'-'+file.originalname)
                    }
                })
              }).single("image")
        ,
        controller.insertData // calling my insert controller
    )

标签: node.jsexpressmulter

解决方案


问题是path您在destination of 中定义的multer应该没有定义path.join()

router.route("/").post(
            multer({
                //dest: 'uploads/expense'
                storage: multer.diskStorage({
                    destination: (req, file, next) => {
                      next(null,'uploads/expense') // HEREEE!
                    },
                    filename: (req, file, next) => {
                        next(null, file.fieldname +'-'+file.originalname)
                    }
                })
              }).single("image")
        ,
        controller.insertData // calling my insert controller
    )

推荐阅读