1. Code
  2. JavaScript
  3. Node

File Upload With Multer in Node.js and Express

Scroll to top

When a web client uploads a file to a server, it is generally submitted through a form and encoded as multipart/form-data. Multer is Express middleware used to handle this multipart/form-data when your users upload files.

In this tutorial, I'll show you how to use the Multer library to handle different file upload situations in Node.

How Does Multer Work?

As I said above, Multer is Express middleware. Middleware is a piece of software that connects different applications or software components. In Express, middleware processes and transforms incoming requests to the server. In our case, Multer acts as a helper when uploading files.

Project Setup

We will be using the Node Express framework for this project. Of course, you'll need to have Node installed.

Create a directory for our project, navigate into the directory, and run npm init -y to create a package.json file that manages all the dependencies for our application. The -y suffix—also known as the yes flag—is used to accept the default values that come from npm init prompts automatically.

1
mkdir upload-express
2
cd upload-express
3
npm init -y

Next, we will install Multer, Express, and the other dependencies necessary to bootstrap an Express app.

1
npm install express multer --save

We will create a server.js file:

1
touch server.js

In the server.js file, we will initialise all the modules, create an Express app, and create a server for connecting to browsers.

1
// require the installed packages

2
const express = require('express')
3
const multer = require('multer');
4
5
//CREATE EXPRESS APP

6
const app = express();
7
8
//ROUTES WILL GO HERE

9
app.get('/', function(req, res) {
10
    res.json({ message: 'WELCOME' });   
11
});
12
13
app.listen(3000, () => 
14
    console.log('Server started on port 3000')
15
);

After requiring packages and listening to our server on port:3000, we can run the snippet node server.js as this runs our server locally. Navigate to localhost:3000 on your browser and you should see the following message.

welcome messagewelcome messagewelcome message

Create the Client Code

When we are sure our server is running fine, the next thing will be to create an index.html file to write all the code that will be served to the client.

1
touch index.html

This file will contain the different forms that we will use for uploading our different file types.

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
  <meta charset="UTF-8">
5
  <title>MY APP</title>
6
</head>
7
<body>
8
  
9
10
 <!--  SINGLE FILE -->
11
<form action="/uploadfile" enctype="multipart/form-data" method="POST"> 
12
   <input type="file" name="myFile" />
13
   <input type="submit" value="Upload a file"/>
14
</form>
15
16
17
<!-- MULTIPLE FILES -->
18
19
<form action="/uploadmultiple"  enctype="multipart/form-data" method="POST">
20
  Select images: <input type="file" name="myFiles" multiple>
21
  <input type="submit" value="Upload your files"/>
22
</form>
23
24
  <!--   PHOTO-->
25
26
<form action="/uploadphoto" enctype="multipart/form-data" method="POST"> 
27
  <input type="file" name="myImage" accept="image/*" />
28
  <input type="submit" value="Upload Photo"/>
29
</form>
30
31
32
33
</body>
34
</html>
35

Open the server.js file and write a GET route that renders the index.html file instead of the "WELCOME" message.

1
// ROUTES

2
app.get('/',function(req,res){
3
  res.sendFile(__dirname + '/index.html');
4
});

Multer Storage

The next thing will be to define a storage location for our files. Multer gives the option of storing files either in memory (multer.memoryStorage) or to disk (memory.diskStorage). For this project, we will store the files to disk.

For the disk storage, we have two objects: destination and filename. destination is used to tell Multer where to upload the files, and filename is used to name the file within the destination.

To create our destination directory, run the code snippet below to create a new folder called uploads:

1
mkdir uploads

Here, we'll add the following code snippets to the server.js file.

1
//server.js
2
3
// SET STORAGE
4
var storage = multer.diskStorage({
5
  destination: function (req, file, cb) {
6
    cb(null, 'uploads')
7
  },
8
  filename: function (req, file, cb) {
9
    cb(null, file.fieldname + '-' + Date.now())
10
  }
11
})
12
13
var upload = multer({ storage: storage })

Handling File Uploads

Uploading a Single File

In the index.html file, we defined an action attribute that performs a POST request. Now we need to create an endpoint in the Express application. Open the server.js file and add the following code snippet:

1
app.post('/uploadfile', upload.single('myFile'), (req, res, next) => {
2
  const file = req.file
3
  if (!file) {
4
    const error = new Error('Please upload a file')
5
    error.httpStatusCode = 400
6
    return next(error)
7
  }
8
    res.send(file)
9
 
10
})

Note that the name of the file input should be the same as the myFile argument passed to the upload.single function.

Uploading Multiple Files

Uploading multiple files with Multer is similar to a single file upload, but with a few changes. The upload.array method accepts two parameters, which are the required field name myFiles and the maximum count of files 12.

1
//Uploading multiple files

2
app.post('/uploadmultiple', upload.array('myFiles', 12), (req, res, next) => {
3
  const files = req.files
4
  if (!files) {
5
    const error = new Error('Please choose files')
6
    error.httpStatusCode = 400
7
    return next(error)
8
  }
9
    res.send(files)
10
})

Uploading Images

Instead of saving uploaded images to the file system, we'll store them in a MongoDB database so that we can retrieve them later as needed. But first, let's install MongoDB.

1
npm install mongodb --save

We will then connect to MongoDB through the Mongo.client method and then add the MongoDB URL to that method. You can use a cloud service like mLab, which offers a free plan, or simply use the locally available connection. In this tutorial, we'll use the locally available connection, as shown below. We'll include the code snippets below in the server.js file:

1
const MongoClient = require('mongodb').MongoClient
2
const myurl = 'mongodb://localhost:27017';
3
4
MongoClient.connect(myurl, (err, client) => {
5
  if (err) return console.log(err)
6
  db = client.db('test') 
7
  app.listen(3000, () => {
8
    console.log('Database connected successfully')
9
    console.log('Server started on port 3000')
10
  })
11
})

We need to read the file path of our request. In Node.js, we can use the file-system (fs) module to read the content of a file. We have to install the file-system dependency:

1
npm install file-system --save

The file-system module is a built-in module which has the methods readFile() and readFileSync(). In this tutorial, we'll use the readFileSync() method. The readFileSync() method is used to read the content of a file synchronously, which means that the method has to be finished before the program execution continues. This method accepts the parameter path, which is the relative path of the file you want to read.

Open server.js, require the file-system dependency, and define a POST request that enables the saving of images to the database.

1
const fs = require('file-system');
2
3
app.post('/uploadphoto', upload.single('myImage'), (req, res) => {
4
    var img = fs.readFileSync(req.file.path);
5
    var encode_image = img.toString('base64');
6
    // Define a JSONobject for the image attributes for saving to database

7
8
    var finalImg = {
9
        contentType: req.file.mimetype,
10
        image: Buffer.from(encode_image, 'base64')
11
    };
12
     db.collection('myCollection').insertOne(finalImg, (err, result) => {
13
        console.log(result)
14
        if (err) return console.log(err)
15
        console.log('saved to database')
16
        res.redirect('/')
17
    })
18
})

In the above code, we first encode the image to a base64 string, construct a new buffer from the base64 encoded string, and then save it to our database collection in JSON format.

We then display a success message and redirect the user to the index page.

Retrieving Stored Images

To retrieve the stored images, we perform a MongoDB search using the find method and return an array of results. We then obtain the _id attributes of all the images and return them to the user.

1
app.get('/photos', (req, res) => {
2
    db.collection('myCollection').find().toArray((err, result) => {
3
        const imgArray = result.map(element => element._id);
4
        console.log(imgArray);
5
        if (err) return console.log(err)
6
        res.send(imgArray)
7
8
    })
9
});

Since we already know the ids of the images, we can view an image by passing its id in the browser, as illustrated below.

1
const ObjectId = require('mongodb').ObjectId;
2
3
app.get('/photo/:id', (req, res) => {
4
    var filename = req.params.id;
5
    db.collection('myCollection').findOne({ '_id': ObjectId(filename) }, (err, result) => {
6
        if (err) return console.log(err)
7
        res.contentType('image/jpeg');
8
        res.send(result.image.buffer)
9
    })
10
})

Saved imageSaved imageSaved image

Conclusion

I hope you found this tutorial helpful. File upload can be an intimidating topic, but it doesn't have to be hard to implement. With Express and Multer, handling multipart/form-data is quite straightforward.

You can find the full source code for the file upload example in our GitHub repo.

This post has been updated with contributions from Mary Okosun. Mary is a software developer based in Lagos, Nigeria, with expertise in Node.js, JavaScript, MySQL, and NoSQL technologies.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.