如何使用 multer 将图像保存在磁盘上并将图像路径保存在 mongodb 上?

How do I save an image on disk and the images path on mongodb with multer?

到目前为止,我设法制作了一张要上传到 mongo 上的图像,但我无法将其保存到磁盘上。

React 组件

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.

上的图像

第一:您正在使用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,所以您无权访问 buffer 根据 docs。请阅读文档,因为显然您在此处使用了错误的上传文件属性:

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

您必须将其替换为:

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