Created
June 12, 2020 17:43
-
-
Save kaungmyatlwin/c9fa34fe669e2ba6d195696649280a77 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 router = require('express').Router(); | |
const upload = require('./upload')('./cats'); | |
router.post('/', upload.array('photos', 10), (req, res) => { | |
// handle logic | |
res.send('ok'); | |
}); | |
module.exports = router; |
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 router = require('express').Router(); | |
const upload = require('./upload')('./dogs'); | |
router.post('/', upload.array('photos', 10), (req, res) => { | |
// handle logic | |
res.send('ok'); | |
}); | |
module.exports = router; |
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'); | |
function customStore(dirPath) { | |
return multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, dirPath); | |
}, | |
filename: (req, file, cb) => { | |
const fname = `${Date.now()}-${encodeURIComponent(file.originalname)}`; | |
cb(null, fname); | |
}, | |
}); | |
} | |
const upload = (dirPath) => multer({ | |
storage: customStore(dirPath), | |
}); | |
module.exports = upload; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment