Created
January 11, 2017 12:04
-
-
Save aitchkhan/00aac9714022c5cd31d2acec89450769 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 multer = require('multer'); | |
const path = require('path'); | |
const config = require('../config/index'); | |
const pathToUploads = config.uploadDestination; | |
const limits = { | |
fileSize: 5242880 | |
} | |
const storage = multer.diskStorage({ | |
destination: function (req, file, cb) { | |
cb(null, pathToUploads); | |
}, | |
filename: function (req, file, cb) { | |
cb(null, `${ file.fieldname }-${ Date.now() }${ path.extname(file.originalname) }`); | |
} | |
}); | |
/** todo: file filter to accept only images | |
* */ | |
function fileFilter(req, file, cb) { | |
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) { | |
return cb(new Error('Only image files are allowed!')); | |
} | |
cb(null, true); | |
} | |
const upload = multer({ storage, limits }); | |
module.exports = upload; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is how multer accept any file type but this is for single file upload.