Skip to content

Instantly share code, notes, and snippets.

@lazycipher
Created November 26, 2020 14:20
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 lazycipher/0a6ad30bc925ecdb0e56ef1cae169051 to your computer and use it in GitHub Desktop.
Save lazycipher/0a6ad30bc925ecdb0e56ef1cae169051 to your computer and use it in GitHub Desktop.
Convert File to buffer using multer
// type of files allowed
const fileFilter = (req, file, cb) => {
if (file.originalname.match(/\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/)) {
cb(null, true)
} else {
cb(null, false)
}
}
const storage = multer.memoryStorage()
exports.processFile = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 10 // 10 mb
},
fileFilter: fileFilter,
upload: (err) => {
if (err instanceof multer.MulterError) {
throw new Error('error in uploading ' + err)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment