【问题标题】:How do I save an image on disk and the images path on mongodb with multer?如何使用 multer 将图像保存在磁盘上和 mongodb 上的图像路径?
【发布时间】:2020-10-30 23:18:14
【问题描述】:

到目前为止,我设法将图像上传到 mongo,但无法将其保存在磁盘上。

反应组件

const handleUploadPhoto = evt => {
    evt.preventDefault();

    const uploads = photos.map(async photo => {
        const formData = new FormData();
        formData.append('photo', photo);

        await axios.post(
            `${baseUrl}/api/photo/upload/${JSON.parse(localStorage.getItem('loggedUser'))._id}`,
            formData,
            { headers: {
                'Content-Type': 'multipart/form-data'
            }},
        );
    });
};

使用 multer 设置上传路由:

const FILE_PATH = 'uploads';

const storage = multer.diskStorage({
    destination: (req, file, cb) => cb(null, FILE_PATH),
    filename: (req, file, cb) => {
        const newFileName = `${Math.floor(Math.random() * 1000000)}-${file.originalname.split('.')[0]}${path.extname(file.originalname)}`;
        cb(null, newFileName);
    },
});

const upload = multer({
    storage,
    fileFilter (req, file, cb) {
        if (!file.originalname.match(/\.(jpeg\jpg\png)$/)) {
            cb(new Error('Only upload jpg and png files.'));
        }

        cb(undefined, true);
    }
});

router.post('/upload/:userid', upload.single('photo'), uploadPhoto);

还有控制器:

const { name, data, mimetype } = req.files.photo;

 User
    .findById(req.params.userid)
    .exec((error, user) => {
        if (error || !user) return res.status(404).json({ message: error.message, });

        const newPhoto = new Photo({
            photo: {
                mimetype,
                data,
                path: `${__dirname}/uploads/${name}`,
                name,
            },
            owner: user._id,
        });

        newPhoto.save((error, photo) => {
            if (error) return res.status(401).json({ message: error, });

            user.photos.push(newPhoto._id);
            user.save();

            return res.status(201).json({
                message: `Image created`,
                path: photo.path,
            });
        });
    });

所以,如您所见,我将数据保存为 mongodb 中的缓冲区,我想实际将图像保存在磁盘上,并且只将图像的名称、mymetype 和路径(磁盘上的位置)保存在 mongodb 上。

【问题讨论】:

    标签: mongodb express multer mern


    【解决方案1】:

    首先:您正在使用upload.single() 获取上传的文件

    router.post('/upload/:userid', upload.single('photo'), uploadPhoto);
    

    而upload.single() 会将文件附加到req.file 而不是req.files。其中,您使用req.files获取上传的文件,用于上传多个文件:

    const { name, data, mimetype } = req.files.photo;
    

    第二:__dirname获取当前文件的路径而不是当前工作目录for more info,所以你不应该使用__dirname,而是使用./。所以我假设您实际上将文件保存到不同的目录,但使用错误的路径保存在 Mongo 中。

    第三:您正在使用multer.diskStorage,它会自动将文件保存到磁盘,并且由于您使用的是diskStorage,因此您无法根据docs访问buffer。阅读文档,因为您显然在这里使用了错误的上传文件属性:

    const { name, data, mimetype } = req.files.photo;
    

    您必须将其替换为:

    const { filename, mimetype } = req.file; // don't have access to buffer in diskStorage
    

    【讨论】:

    • 是的,伙计,很好的解释。现在可以了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 2011-10-31
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2012-11-17
    相关资源
    最近更新 更多