welcome

File Upload With Multer in Node.js and Express

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 middleware for Express and Node.js that makes it easy to handle this multipart/form-data when your users upload files. In this tutorial, I’ll show you how to use this 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 component. 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 issue npm init to create a .json file that manages all the dependencies for our application.

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

Next, create a server.js file. 

Then, in server.js, we will initialize all the modules, create an express app and create a server for connecting to browsers.

Running node server.js and navigating to localhost:3000 on your browser should give you the following message.

welcome message

Create the Client Code

The next thing will be to create an index.html file to write all the code that will be served to the client.

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

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

Multer storage

The next thing will be to define a storage location for our files. Multer gives the option of storing files to disk as shown below. Here we set up a directory where all our files will be saved and we’ll also give the files a new identifier.

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:

Note that the name of the file field 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 a few changes.

Uploading Images

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

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.

Open server.js and define a POST request that enables saving images to the database.

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 go on and obtain the _id attributes of all the images and return them to the user.

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

Saved 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 easy and straight forward. You can find the full source code for the file upload example in our GitHub repo.

Powered by WPeMatico

Leave a Comment

Scroll to Top