首页 > 解决方案 > node.js中如何使用memoryStorage()上传文件

问题描述

我正在学习如何在 node.js 中上传文件,当我使用 multer.diskStorage 时,我能够将文件上传到系统中的本地文件夹

const storag = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'uploads');
    },
    filename: (req, file, cb) => {
        const { originalname } = file;
        cb(null, originalname);
    }
});
const upload = multer({ storage: storag });
router.post("/lecture", upload.single('file'), (req, res) => {
    return res.json({ status: 'OK' });
});

但是一旦我将 multer.diskStorage 更改为 multer.memoryStorage 因为我想直接发布到谷歌云存储,服务器返回相同的路由并出现 404 错误,我想知道问题是什么

const storag = multer.memoryStorage()
const upload = multer({ storage: storag });
router.post("/lecture", upload.single('file'), instructorController.createLecture, (req, res) => {
    return res.json({ status: 'OK' });
});

标签: node.jsmulter

解决方案


推荐阅读