Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filefilter의 위치 잘못되어있습니다. #18

Open
idpokute opened this issue Oct 14, 2020 · 0 comments
Open

filefilter의 위치 잘못되어있습니다. #18

idpokute opened this issue Oct 14, 2020 · 0 comments

Comments

@idpokute
Copy link

먼저 좋은 강의 감사합니다.

fileFilter의 위치가 multer.diskstorage에 넣은경우 그냥 무시가 되서 아무파일이나 업로드가 됩니다. 공식 문서에 따르면 multer가 올바른 위치인 것같습니다.
또한 const path = require('path'); 도 필요해 보입니다.

일단 jpg로 테스트해 본 코드를 적어보았습니다.

const path = require('path');
const multer = require('multer');
let storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'uploads/');
  },
  filename: (req, file, cb) => {
    console.log(file.mimetype);
    cb(null, `${Date.now()}_${file.originalname}`);
  },
});
const upload = multer({
  storage: storage,
  fileFilter: (req, file, cb) => {
    const ext = path.extname(file.originalname);

    if (ext !== '.jpg') {
      console.log('it is not jpg');
      return cb(new Error('only jpg is allowed'), false);
    }
    cb(null, true);
  },
}).single('file');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant