使用Multer存储图像,并仅将路径存储在MongoDB中



所以我的架构的一部分看起来像这个photo: [{data: Buffer, contentType: String }]

Multer这样存储图像:

var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/uploads');
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now());
}
});
app.use(multer({
storage: storage
}).single('photo'));

现在我知道它将图像作为缓冲区存储在Mongodb中,并将文件存储在服务器上。我希望它将文件的路径存储在数据库中,然后让它将实际的图像文件存储在服务器上。它将图像存储为文本文件,看起来是用utf-8编码的。对于一个看似简单的任务(存储图像(来说,这是一个非常复杂的过程

您可以使用req.file.path访问文件的路径

app.post("/file", function(req, res) {
console.log(req.file.path)
})

最新更新