Express.js File Upload – javaTpoint

Express.js File Upload

In Express.js, file upload is slightly difficult because of its asynchronous nature and networking approach.

It can be done by using middleware to handle multipart/form data. There are many middleware that can be used like multer, connect, body-parser etc.

Let’s take an example to demonstrate file upload in Node.js. Here, we are using the middleware ‘multer’.

Create a folder “jtp file upload” having the following files:

Express.js file upload 1

uploads: It is an empty folder i.e. created to store the uploaded images.

package: It is JSON file, having the following data:

File: package.json

File: index.html

File: server.js

To install the package.json, execute the following code:

Express.js file upload 2

It will create a new folder “node_modules” inside the “jtp file upload” folder.

Express.js file upload 3

Dependencies are installed. Now, run the server:

Express.js file upload 4

Open the local page http://127.0.0.1:2000/ to upload the images.

Express.js file upload 5

Select an image to upload and click on “Upload Image” button.

Express.js file upload 6

Here, you see that file is uploaded successfully. You can see the uploaded file in the “Uploads” folder.

Express.js file upload 7

Download Node.js Express File Upload Example

Download this example

Next Topic

ExpressJS Middleware