Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Express Documentation22টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Routing
  • Middleware
  • Request & Response
  • Static Files
  • Router
  • Error Handling
  • Template Engines
  • Body Parsing
  • File Upload
  • CORS
  • Database
  • Validation
  • Authentication
  • Security
  • REST API
  • Performance
  • Testing
  • Best Practices
  • Project
  • Interview
  1. Docs
  2. Express
  3. Express File Upload (Multer)
ExpressIntermediate

Express File Upload (Multer)

Multer install, single/multiple upload, validation, static serve — বাংলায়।

11টি অংশবাংলা ভাষায়

Multer হলো Node.js-এ file upload-এর সবচেয়ে জনপ্রিয় library। এটি multipart/form-data handle করে।

Multer Setup

npm install multer
config/upload.jsjavascript
javascript
const multer = require("multer");
const path = require("node:path");

// Storage configuration
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "uploads/");
  },
  filename: (req, file, cb) => {
    const ext = path.extname(file.originalname);
    const unique = Date.now() + "-" + Math.round(Math.random() * 1e9);
    cb(null, file.fieldname + "-" + unique + ext);
  },
});

// File filter — types
const fileFilter = (req, file, cb) => {
  const allowed = /jpeg|jpg|png|gif|webp/;
  const ok = allowed.test(path.extname(file.originalname).toLowerCase());
  cb(null, ok);
};

// Multer instance
const upload = multer({
  storage,
  fileFilter,
  limits: { fileSize: 5 * 1024 * 1024 }, // 5MB
});

module.exports = upload;

Single File Upload

const upload = require("./config/upload");

// avatar field-এ one file
app.post("/api/avatar", upload.single("avatar"), (req, res) => {
  if (!req.file) {
    return res.status(400).json({ error: "ফাইল লাগবে" });
  }
  res.status(201).json({
    message: "Upload সফল",
    file: {
      name: req.file.filename,
      size: req.file.size,
      url: "/uploads/" + req.file.filename,
    },
  });
});

Multiple Files

// একই field-এ একাধিক
app.post("/api/gallery", upload.array("photos", 10), (req, res) => {
  res.json({ count: req.files.length });
});

// একাধিক field — avatar + cover
app.post("/api/profile", upload.fields([
  { name: "avatar", maxCount: 1 },
  { name: "cover", maxCount: 1 },
]), (req, res) => {
  res.json({
    avatar: req.files["avatar"][0],
    cover: req.files["cover"][0],
  });
});

Uploaded Files Serve করা

const app = express();
const path = require("node:path");

// uploads/ folder থেকে files serve
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
Security

Uploaded ফাইলের extension ও size সবসময় validate করুন। আসল সিস্টেমে Cloudinary বা S3 bucket-এ ফাইল store করা best practice।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরExpress Body ParsingপরেরExpress CORS
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com