Node js File Upload Express using Multer

Posted at: March 24, 2019 2:46 PM

How to uplode single or multiple files in node js with more fields

In this chapter we will learn node js file upload, node js multer file upload with example, node js multiple file upload, node js express upload image example, node js rest api file upload, multer multiple file upload. We will use express-generator tool for creating a application. And we will use view engine hbs.

First of all, we will install express-generator tools globaly. If you have already installed express-generator tool then no need to install.

Intallation express-generator tool

Create a Application

Create a application using express-generator tool and set view engine hbs

Then install dependencies

Install multer

Multer is a node.js middleware for handling multipart/form-data, which is used for uploading files.

Create a blogs.js in routes folder for upload files.

Edit app.js

Edit app.js file and import var blogsRouter = require('./routes/blogs'); and set blogs route app.use('/blogs', blogsRouter);

DiskStorage

The disk storage engine gives you full control on storing files to disk.

Single File Upload

Import multer into routes/blogs.js for file upload.

routes/blogs.js

View File

Create a view file create.hbs in viewsfolder.

views/create.hbs

Multiple Files Upload

In multiple files upload, accept an array of files, all with the name fieldname and number of files. In this code upload.array('blogimage', 5) blogimage is fieldname< and 5 is number of files

Add multiple in html form

Complete Code

rotues/blogs.js

Run the app with this command

Then load http://localhost:3000/ in your browser to access the app.

File upload url http://localhost:3000/blogs/create

Conclusion

In this lesson we have learned installation of express-generator tool and create application using express-generator tool. Upload single and multiple file using multer in Node.js.

You can watch video on Node js File Upload Express using Multer


node js file upload node js multiple file upload multer express

Please leave comments

0 Comments

truecodex launched free android app. You can download and install truecodex app on your mobile and get notification of the latest post.

Video Tutorials on YouTube