如何解决React 应用程序 - 文件上传适用于本地主机,但不适用于 nginx 服务器
希望能帮到你。
当我在本地主机上运行我的应用程序时,以下代码有效,文件正确上传到应用程序根目录中的上传文件夹。但是,当我尝试从实时网站上传文件时,会出现错误。
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,`${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,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:没有这样的文件或目录,打开'build/public/uploads/1619823055971_GettyImages-640318118-c83a508.jpg'
是否有一个文件夹需要我在运行构建以进行 react.js 后设置用于上传?刚才我在项目的根目录下只有一个uploads文件夹。
感谢您的建议
解决方法
看起来我没有重新启动 pm2,所以它没有显示我对 express 文件所做的任何更改。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。