簡體   English   中英

React 應用程序 - 文件上傳適用於本地主機,但不適用於 nginx 服務器

[英]React application - file upload works on localhost but not on nginx server

希望你能幫忙。

當我在本地主機上運行我的應用程序時,以下代碼有效,文件正確上傳到應用程序根目錄中的上傳文件夾。 但是,當我嘗試從我的實時網站上傳文件時,會出現錯誤。

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:沒有這樣的文件或目錄,打開'build/public/uploads/1619823055971_GettyImages-640318118-c83a508.jpg'

運行構建以進行反應后,是否需要為上傳設置一個文件夾。 剛才我在項目的根目錄下只有一個上傳文件夾。

感謝您的任何建議

看起來我沒有重新啟動 pm2,所以它沒有顯示我對 express 文件所做的任何更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM