const multer = require("multer"); const path = require("path"); const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, "./server-side/public/uploads"); }, filename: function (req, file, cb) { const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9); cb( null, file.fieldname + "-" + uniqueSuffix + path.extname(file.originalname) ); }, }); const fileFilter = (req, file, cb) => { if ( file.mimetype === "image/jpeg" || file.mimetype === "image/png" || file.mimetype === "image/jpg" || file.mimetype === "image/gif" ) { cb(null, true); } else { cb(null, false); } }; const upload = multer({ storage: storage, limits: { fileSize: 1024 * 1024 * 5, // max 5mb }, fileFilter: fileFilter, }); module.exports = upload;
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