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
What is Multer in NodeJs?
How to use Multer in node.Js?
Multer module is used for file uploading in node.Js. there are various module available in node.Js for file uploading.
Multer is a middleware which is used for handling file uploading(multipart/form-data).
There are two ways to upload forms with multipart/form-data encoding.
The FormData API allows us to build a multipart/form-data form with key-value pairs that can be sent to the server.
const frmd = new FormData()
frmd.append('name', "Dillion")
frmd.append('image', <a file>)
Multer does the work of body-parser by attaching the values of text fields in the req.body object. Multer also creates a new object for multiple files, either req.file or req.files, which holds information about those files.
How to install multer?
File Name :
npm install multer
############## OR ##############
npm install --save multer
Check Version of multer
File Name :
npm version multer
Requiring module:
File Name :
var multer = require('multer');
Multer basically adds a file object or files object and a body object to the request object.
The file/files object contains all the files which are uploaded through the form and all the values of the text fields of the form are contained in the body object.
Html Form for File uploading :-
For uploading file You have to must declare three main attributes within the form tag.
File Name :
action=/upload_file" :- when you submit the form It will redirect to /upload_file action through the POST method.
method="POST" :- post method is user to send form information on action page.
enctype="multipart/form-data" :- enctype handles file uploading request.
<form action="/upload_file" method="POST" enctype="multipart/form-data">
input field :-
html input tag containg mainly three attributes for file uploading.
File Name :
type="file" – input type file is used to creates a browse Button to select any files from your device.
name="myfile" – name is used to access file information.
multiple – multiple is optional. when you want to upload multiple file then write multiple. It allows us to select multiple files/images at a time
<input type="file" name="myfile" multiple>
Example :- Html form for file upload in node.js
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="/upload_file" method="POST" enctype="multipart/form-data">
<label>Store Files</label><br><br>
<p><%=(typeof alertMsg!='undefined')? alertMsg:''%></p>
<input type="file" name="myfile" multiple>
<button type="submit">Upload</button>
</form>
</body>
</html>