用 Multer 存储图片,只存储 MongoDB 中的路径
Store images with Multer and store only the path in MongoDB
所以我的 Schema 的一部分看起来像这样 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)
})
所以我的 Schema 的一部分看起来像这样 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)
})