NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
How to Upload multiple file using node.js and Express.Js with multer module
Create a Project
c:\> npx express --view=ejs crud_api
Initialise and Configure Our Project
File Name :
c:\crud_api> npm init -y
npm install
c:\crud_api> npm install
Install express and other dependencies
File Name :
c:\crud_api> npm install express --save
install Body Parser :-
File Name :
c:\crud_api> npm install body-parser --save
MySql Install
File Name :
c:\crud_api> npm install mysql --save
Nodemon Install
File Name :
c:\crud_api> npm install --save-dev nodemon
Install Multer
File Name :
npm install --save multer
create upload-form.js page under view directory.
File Name : views/upload_form.js
<!DOCTYPE html>
<html>
<head></head>
<body>
<form action="/upload_myfile" method="POST" enctype="multipart/form-data">
<label>Avatar</label>
<input type="file" name="myfiles" multiple>
<button type="submit">Upload</button>
</form>
</body>
</html>
creates directory middlewares under root directory.
create a file upload-middleware.js in the middlewares folder.
You must include the Multer module by using require('multer')
File Name : middlewares/upload_middleware.js
var multer = require('multer');
module.exports.files={
storage:function(){
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/files/')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
return storage;
},
allowedFiles:function(req, file, cb) {
// Accept images only
if (!file.originalname.match(/\.(pdf|doc|txt|jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/)) {
req.fileValidationError = 'Only pdf|doc|txt|jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF file type are allowed!';
return cb(new Error('Only pdf|doc|txt|jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF file type are allowed!'), false);
}
cb(null, true);
}
}
Create Controller :-
include a middleware file using require('../middlewares/upload_middleware')
File Name : controllers/upload_controller.js
var multer = require('multer');
var uplm_obj = require('../middlewares/upload_middleware');
module.exports={
uploadForm:function(req,res){
res.render('upload_form');
},
uploadFiles:function(req,res){
var upload = multer({
storage: uplm_obj.files.storage(),
allowedFiles:uplm_obj.files.allowedFiles
}).single('files');
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
res.send(err);
} else if (err) {
res.send(err);
}else{
res.render('upload_form');
}
})
}
}
Create Routes to Upload Multiple Files
File Name : routes/upload_route.js
const express = require('express');
const router = express.Router();
const upc = require('../controllers/upload_controller');
router.get('/upload-files',upc.uploadForm);
router.post('/upload-files',upc.uploadFiles);
module.exports = router;
Include the Router File in app.js
File Name : app.js
var ur = require('./routes/upload_route');
;
app.use('/', ur);
Output :-
http://localhost:3000/upload-files
open your browser and Enter the above URL in your browser
after that Select multiple files from your device by pressing the ctrl/shift key
finally Click submit button to upload files successfully.