Reactjs 如何让multer将图像保存到磁盘?
因此,我一直在尝试让multer将图像数据保存到磁盘上,并将图像的mimetypes、名称和路径保存到Mongo上磁盘上的文件夹中。我现在看到的是这样的: 多媒体设置:Reactjs 如何让multer将图像保存到磁盘?,reactjs,mongodb,express,multer,Reactjs,Mongodb,Express,Multer,因此,我一直在尝试让multer将图像数据保存到磁盘上,并将图像的mimetypes、名称和路径保存到Mongo上磁盘上的文件夹中。我现在看到的是这样的: 多媒体设置: const storage = multer.diskStorage({ destination: (req, file, cb) => cb(null, '/public/images/uploads'), filename: (req, file, cb) => { cb(null
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, '/public/images/uploads'),
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname)
},
});
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 newPhoto = new Photo({
photo: {
mimetype,
photoUrl: `images/uploads/${name}`,
name,
},
});
await axios.post(
`${baseUrl}/api/photo/upload/${JSON.parse(localStorage.getItem('loggedUser'))._id}`,
formData,
{ headers: {
'Content-Type': 'multipart/form-data'
}},
).then(response => setPhotoUrls([response.data.photoUrl, ...photoUrls]));
上传图像的路径:
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, '/public/images/uploads'),
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname)
},
});
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 newPhoto = new Photo({
photo: {
mimetype,
photoUrl: `images/uploads/${name}`,
name,
},
});
await axios.post(
`${baseUrl}/api/photo/upload/${JSON.parse(localStorage.getItem('loggedUser'))._id}`,
formData,
{ headers: {
'Content-Type': 'multipart/form-data'
}},
).then(response => setPhotoUrls([response.data.photoUrl, ...photoUrls]));
然后从控制器中选择此位,以便在Mongo上保存图像详细信息:
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, '/public/images/uploads'),
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname)
},
});
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 newPhoto = new Photo({
photo: {
mimetype,
photoUrl: `images/uploads/${name}`,
name,
},
});
await axios.post(
`${baseUrl}/api/photo/upload/${JSON.parse(localStorage.getItem('loggedUser'))._id}`,
formData,
{ headers: {
'Content-Type': 'multipart/form-data'
}},
).then(response => setPhotoUrls([response.data.photoUrl, ...photoUrls]));
对“上传”端点的axios调用:
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, '/public/images/uploads'),
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname)
},
});
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 newPhoto = new Photo({
photo: {
mimetype,
photoUrl: `images/uploads/${name}`,
name,
},
});
await axios.post(
`${baseUrl}/api/photo/upload/${JSON.parse(localStorage.getItem('loggedUser'))._id}`,
formData,
{ headers: {
'Content-Type': 'multipart/form-data'
}},
).then(response => setPhotoUrls([response.data.photoUrl, ...photoUrls]));
因此后端部分工作正常,因为它将所需的数据保存在Mongo上,但图像仍然无法保存在磁盘上。如何让multer将所选图像保存到磁盘上?为multer创建seprate文件夹,如
multerHelper.js
const multer = require('multer');
let fs = require('fs-extra');
let storage = multer.diskStorage({
destination: function (req, file, cb) {
fs.mkdirsSync(__dirname + '/uploads/images'); // fs.mkdirsSync will create folders if it does not exist
cb(null, __dirname + '/uploads/images');
},
filename: function (req, file, cb) {
console.log(file);
cb(null, Date.now() + '-' + file.originalname);
}
})
let upload = multer({ storage: storage });
let createUserImage = upload.single('photo');
let multerHelper = {
createUserImage,
}
module.exports = multerHelper;
在路由中导入multerhelper文件
const multerHelper = require("../helpers/multer_helper");
router.post('/upload/:userid',multerHelper, uploadPhoto);