Last active
May 16, 2021 20:23
-
-
Save Ndohjapan/fb3be032b215261e129ed5f684bf9333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const upload = multer({ | |
storage: multer.diskstorage({ | |
destination: "/upload/images", // Storage location | |
filename: (req, res, (cb) => { | |
cb(null, Date.now() + path.extname(file.originalname)) // return a unique file name for every file | |
}) | |
}), | |
limits: {fileSize: 20000000, // This limits file size to 2 million bytes(2mb) | |
fileFilter: (req, file, cb) => { | |
const validFileTypes = /jpg|jpeg|png/ // Create regex to match jpg and png | |
// Do the regex match to check if file extenxion match | |
const extname = validFileTypes.test(path.extname(file.originalname).toLowerCase()) | |
if(extname === true){ | |
// Return true and file is saved | |
return cb(null, true) | |
}else{ | |
// Return error message if file extension does not match | |
cb("Error: Images Only!") | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment