【发布时间】:2021-04-30 22:52:47
【问题描述】:
希望你能帮忙。
当我在本地主机上运行我的应用程序时,以下代码有效,文件正确上传到应用程序根目录中的上传文件夹。但是,当我尝试从我的实时网站上传文件时,会出现错误。
onst upload = multer({ // Set up multer module to move uploaded file an rename the file
storage: multer.diskStorage({
destination(req, file, cb) {
cb(null, 'uploads/'); //Sets the destination for the new file
},
filename(req, file, cb) {
cb(null, `${new Date().getTime()}_${file.originalname}`); // renames file with the date, time and its original name
}
}),
limits: {
fileSize: 1024 * 1024 * 5 // Sets the max file size
},
fileFilter: (req, file, cb) => {// Only allows image files uploaded
if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "image/jpeg" || file.mimetype === 'image/gif' || file.mimetype ==='image/jfif' ) {
cb(null, true);
} else {
cb(null, false);
return cb( new Error('Only .png, .jpg and .jpeg format allowed!' ));
}
}
});
这是我在运行时遇到的错误错误:ENOENT: no such file or directory, open 'build/public/uploads/1619823055971_GettyImages-640318118-c83a508.jpg'
运行构建进行反应后,是否需要设置一个文件夹以进行上传。刚才我在项目的根目录下只有一个上传文件夹。
感谢您的建议
【问题讨论】:
标签: reactjs express multer nginx-reverse-proxy