File Uploads in NestJS Using Multer

Handling file uploads is a critical part of many web applications—from uploading profile pictures to processing documents. Fortunately, NestJS offers a clean and structured way to handle this using Multer, a powerful middleware for handling multipart/form-data.

In this guide, you’ll learn how to implement file uploads in NestJS using Multer, including single and multiple file uploads, file validation, and storage configuration.

Why Use Multer with NestJS?

Multer integrates seamlessly with NestJS via the @nestjs/platform-express package. It allows you to control storage location, rename files, validate file types and sizes, and limit uploads per route—all while keeping your code modular and clean.

Setting Up Your NestJS Project

If you’re starting from scratch:

Install required packages:

Creating the Upload Module

First, create a module, controller, and service:

Now let’s dive into building the file upload feature.

Single File Upload

In your upload.controller.ts:

Make sure the uploads directory exists or handle its creation.

Multiple File Uploads

To upload more than one file:

This limits the number of uploaded files to 5. You can change that number as needed.

Validating File Types and Size

Use the ParseFilePipeBuilder to add validation:

This ensures only .jpeg files under 1MB are accepted.

Serving Uploaded Files

To serve uploaded files, update main.ts:

Now you can access uploaded files via http://localhost:3000/filename.

Best Practices

  • Store sensitive files in a protected folder, not public.
  • Use UUIDs or hash-based names to avoid collisions.
  • Move files to cloud storage like AWS S3 or Cloudinary for production.
  • Clean up old or unused files periodically.

Final Thoughts

Uploading files in NestJS using Multer is straightforward and flexible. You get full control over how files are stored, validated, and accessed—making it perfect for applications that need to handle user uploads reliably.

For more scalable backend solutions with NestJS and Node.js, check out this backend tech stack guide from Dev Centre House Ireland.