Handling Multiple File Uploads with Multer in Node.js

Introduction

Dev Balaji
5 min readOct 24, 2023

Multer is a popular middleware for handling multipart/form-data in Node.js applications. It is primarily used for processing and managing file uploads. Multer simplifies the process of accepting files as part of an HTTP POST request, processing them, and saving them to a location on your server or cloud storage.

Key features and uses of Multer:

  1. File Uploads: Multer is used to handle file uploads, such as images, videos, documents, and other binary data, from HTML forms. It can process both single and multiple file uploads.
  2. Configuration: Multer allows you to configure various options, such as where to store uploaded files, how to name them, and size limits for uploaded files.
  3. Middleware: Multer is typically used as middleware in Express.js applications. It can be added to specific routes to process file uploads. Multer processes the uploaded files and makes them accessible in the request object for further handling.
  4. Storage Engines: Multer supports different storage engines, including disk storage, memory storage, and cloud storage solutions like Amazon S3. You can choose the storage engine that best suits your project’s needs.
  5. File Validation: Multer can validate uploaded files based on file type, file size, and other criteria. This helps ensure that only acceptable files are processed.

Here’s a step-by-step guide on how to use Multer for file uploads in a Node.js application (Basic Approach):

  1. Set up your Node.js project: Before you start, make sure you have Node.js installed and create a new Node.js project or use an existing one.
  2. Install Multer: You need to install the Multer package using npm or yarn. Open your terminal and run:
npm install multer
# or
yarn add multer

3. Configure Multer in your Node.js application: Import Multer and set up the configuration for handling file uploads. Here’s a basic example:

const express = require('express');
const multer = require('multer');
const app = express();

// Define storage for uploaded files
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/'); // Destination folder for uploaded files
},
filename: (req, file, cb) => {
cb(null,Date.now() + '-' + file.originalname); // Rename the file to include the timestamp
},
});

// Initialize Multer with the storage configuration
const upload = multer({ storage: storage });

app.post('/upload', upload.single('file'), (req, res) => {
// req.file contains the uploaded file details
// req.body contains other form data if any
res.send('File uploaded successfully');
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

In this example, we configure Multer to use the diskStorage engine to store uploaded files on the server's disk. The upload.single('file') middleware is used to handle a single file upload with the field name 'file'.

In case you need to handle a text-only multipart form, you should use the .none() method:

const express = require('express')
const app = express()
const multer = require('multer')
const upload = multer()

app.post('/upload', upload.none(), function (req, res, next) {
// req.body contains the text fields
})

4. Create an HTML form for file upload: In your HTML file, create a form that allows users to select and upload files. Make sure the form’s enctype is set to "multipart/form-data" to handle file uploads.

<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload File">
</form>

To handle text only in your HTML file, create a form that allows users to upload text fields

<form action="/stats" enctype="multipart/form-data" method="post">
<div class="form-group">
<input type="file" class="form-control-file" name="uploaded_file">
<input type="text" class="form-control" placeholder="Number of speakers" name="speakers">
<input type="submit" value="Get me the stats!" class="btn btn-default">
</div>
</form>

5. Handle the file upload in your Node.js route: When a file is uploaded, it will be processed by the route you defined earlier. You can access the uploaded file’s information in req.file. You can then perform additional processing or save the file as needed.

Multer is highly configurable and can handle various use cases, such as handling multiple files, validating file types, and setting file size limits.

Each file contains the following information:

Multer accepts an options object, the most basic of which is the dest property, which tells Multer where to upload the files. In case you omit the options object, the files will be kept in memory and never written to disk.

By default, Multer will rename the files so as to avoid naming conflicts. The renaming function can be customized according to your needs.

The following are the options that can be passed to Multer.

Note:

  1. You are responsible for creating the directory when providing a destination as a function. When passing a string, multer will make sure that the directory is created for you.
  2. Multer will not append any file extension for you, your function should return a filename complete with a file extension.

Warnings:

  1. Make sure that you always handle the files that a user uploads. Never add multer as a global middleware since a malicious user could upload files to a route that you didn’t anticipate. Only use this function on routes where you are handling the uploaded files.
  2. Uploading very large files, or relatively small files in large numbers very quickly, can cause your application to run out of memory when memory storage is used.

Error Handling

When encountering an error, Multer will delegate the error to Express. You can display a nice error page using the standard expressway.

If you want to catch errors specifically from Multer, you can call the middleware function by yourself. Also, if you want to catch only the Multer errors, you can use the MulterError class that is attached to the multer object itself (e.g. err instanceof multer.MulterError).

const multer = require('multer')
const upload = multer().single('avatar')

app.post('/profile', function (req, res) {
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
} else if (err) {
// An unknown error occurred when uploading.
}

// Everything went fine.
})
})

In conclusion, Multer is an essential middleware for Node.js developers looking to seamlessly handle file uploads in their web applications. With its flexibility, configuration options, and support for various storage engines, Multer simplifies the process of receiving, processing and storing files sent by users through HTTP requests.

Whether you are building a simple image upload feature or a complex file management system, Multer empowers you to manage the intricacies of file uploads with ease. Its ability to validate and limit file uploads, coupled with its compatibility with cloud storage services, makes it a versatile tool for a wide range of projects.

By incorporating Multer into your Node.js application, you can ensure efficient and secure file handling, enhancing the user experience and expanding the functionality of your web services. With this powerful middleware, you’ll be well-equipped to tackle various file upload requirements in your Node.js projects.

--

--

Dev Balaji

🚀 Tech Enthusiast | 🌟 Mastering JavaScript & Frameworks | 💡 Sharing Tips & Tricks | 📘 Aspiring Blogger & Architect | 🐍 Python Practitioner