Skip to content

Instantly share code, notes, and snippets.

@joshtrigger
Created October 24, 2019 17:11
Show Gist options
  • Save joshtrigger/c383221d7be335e989eb820c2494975d to your computer and use it in GitHub Desktop.
Save joshtrigger/c383221d7be335e989eb820c2494975d to your computer and use it in GitHub Desktop.
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + '-' + file.originalname)
}
})
const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true)
} else {
//reject file
cb({message: 'Unsupported file format'}, false)
}
}
const upload = multer({
storage: storage,
limits: { fileSize: 1024 * 1024 },
fileFilter: fileFilter
})
module.exports = upload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment