【问题标题】:React application - file upload works on localhost but not on nginx serverReact 应用程序 - 文件上传适用于 localhost 但不适用于 nginx 服务器
【发布时间】: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


    【解决方案1】:

    看起来我没有重新启动 pm2,所以它没有显示我对 express 文件所做的任何更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2012-01-01
      • 1970-01-01
      • 2012-09-09
      • 2016-04-02
      • 1970-01-01
      • 2017-11-09
      相关资源
      最近更新 更多