인프런 커뮤니티 질문&답변
fileFilter가 정상적으로 작동되지 않는것 같습니다.
작성
·
215
2
영상에서는 diskStorage 함수 내부에 fileFilter 함수를 작성하여 파일 확장자에 따라 오류 처리를 하도록 했는데 안되는 것 같아요.
구글링해보니 multer 함수 내부에 fileFilter 함수를 정의해야한다고합니다.
궁금해서 찾아봤습니다.
...
const multerFilter = (req, file, cb) => {
const ext = path.extname(file.originalname);
// 동영상이 아닌 경우 에러 발생
if (ext !== ".mp4") {
return cb(new Error("only mp4 is allwoed!"));
}
// 동영상 파일
cb(null, true);
};
const upload = multer({
storage: storage,
fileFilter: multerFilter
}).single("file");답변 1
1
John Ahn
지식공유자
혹시 필터기능 추가 하고 싶으셔서 그런것인가요 ~ ?
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/')
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}_${file.originalname}`)
},
fileFilter: (req, file, cb) => {
const ext = path.extname(file.originalname)
if (ext !== '.mp4') {
return cb(res.status(400).end('only jpg, png, mp4 is allowed'), false);
}
cb(null, true)
}
})
var upload = multer({ storage: storage }).single("file")
이런식으로 해주시면 됩니다 ~!





