Skip to content

Instantly share code, notes, and snippets.

@aitchkhan
Created January 11, 2017 12:04
Show Gist options
  • Save aitchkhan/00aac9714022c5cd31d2acec89450769 to your computer and use it in GitHub Desktop.
Save aitchkhan/00aac9714022c5cd31d2acec89450769 to your computer and use it in GitHub Desktop.
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;
@aitchkhan
Copy link
Author

this is how multer accept any file type but this is for single file upload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment