admin 管理员组

文章数量: 1184232

上传图片到cloudinary, Error: ENOENT: no such file or directory

当我尝试通过邮递员上传图片并保存在 public/upload_files 文件夹中时,它显示此错误

<body>
<pre>Error: ENOENT: no such file or directory, open &#39;storage/media/1637741568809.png&#39;</pre>
</body>

我的代码:

const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./storage/media");
  },

  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname));
  },
});

const filefilter = (req, file, cb) => {
  if (!file) {
    cb(null, false);
  } else {
    cb(null, true);
  }
};
const upload = multer({
  storage: storage,
  limits: { fieldSize: 10 * 1024 * 1024 },
  fileFilter: filefilter,
});
回答如下:

我遇到了同样的问题,这个配置对我有用:

const multer = require("multer")
    
    const DIR = "./public/"
    
    
    const storage = multer.diskStorage({
        destination: (req, file, cb) => {
            cb(null, DIR)
        },
    }); 
    
    const fileFilter =  (req, file, cb ) =>{
      
      if  (file.mimetype === "image/jepg" || file.mimetype === 'image/png'){
            cb(null, true)
        } 
    
    else {
            //reject file 
            cb({message: "Unsupported file format"}, false)
        }
    }
    
    const upload = multer({
        storage:storage, 
        limits:{fileSize: 1024 * 1024}, 
        fileFilter:fileFilter
    })
    
    
    module.exports = upload;

本文标签: 上传图片到cloudinary Error ENOENT no such file or directory