Skip to content

Instantly share code, notes, and snippets.

@Ndohjapan
Last active May 16, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ndohjapan/fb3be032b215261e129ed5f684bf9333 to your computer and use it in GitHub Desktop.
Save Ndohjapan/fb3be032b215261e129ed5f684bf9333 to your computer and use it in GitHub Desktop.
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