node.js - 在 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
)
解决方案
问题是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
)
推荐阅读
- python - 如何将 Button 连接到新的 Canvas 窗口?
- database - How to handle "more than requested number of rows" in Oracle PL/SQL
- javascript - lit-element 不渲染用于复制/粘贴/打印的嵌套(子)组件
- bash - 输出流到输入
- ios - 如何为多个产品配置 xcode 项目?
- flutter - 在单击项目构建器 Flutter 的行视图时刷新 ListView.builder
- vue.js - 多参数 InertiaJS 路由
- r - 如何根据状态为图表着色
- terminal - tmux:如何判断窗格当前是否通过管道传输?
- swift - SwiftUI Preview 找不到使用过的框架的图像