import multer from 'multer'; // see customize section const upload = multer({dest:'uploads/'}) const app = express(); app.use(bodyParser.urlencoded({extended:true})); app.use(bodyParser.json()); app.post('/contact',upload.single('profile'),(req,res)=>{ console.log(req.file.key); // get the name of the file console.log(req.body); // Other fields are in req.body res.redirect('/'); }) // ---------------------------------- // upload options upload.single("field-name"); upload.array("field-name"); upload.fields([ { name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 } ]) // --------------------------------- // customize multer const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, path.resolve('/uploads')); }, filename: function (req, file, cb) { cb(null, `${file.fieldname}-${Date.now()}.${file.mimetype.split('/')[1]}`); } }) const upload = multer({storage})
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter