Multer Multiple File Uploads in One Form

Welcome folks today in this blog mail service nosotros will be looking on how to upload multiple files present in multiple class fields in node.js and express using the multer library. All the source code of the case volition be shown beneath.

Become Started

In society to become started you need to install node.js on your computer and and then execute the below commands to become started with the node.js and express project

npm init -y

npm i express

npm i ejs

npm i multer

npm i nodemon

Limited will be the main node.js server which volition be running the main application

ejs It is the template engine which express uses to render out html documents in limited

multer It is the actual file upload library which is used to upload files

nodemon It is a special dependency which automatically restarts the application for you when you make any changes to it.

Now afterwards installing all these dependencies a package.json file will be created in the root binder. At present you merely need to make a alphabetize.js file in the root binder which will exist the starting indicate of the application

index.js

At present hither in this block of code we are loading the required dependencies which are express and ejs template engine and we are starting the awarding at port 5000

At present we are loading a template alphabetize.ejs so for that you need to create this template in the views binder.

Now create a views folder in the root folder like this views/alphabetize.ejs and create a file called as index.ejs

index.ejs

At present when you launch your node.js app by running

node index.js

y'all will see this screenshot

Now you can meet hither nosotros have multiple file input fields nosotros need to accept the input from the user and upload these files to the express server. For storing information technology we need some folders that we need to create right now.

At present get to the root folder create a public/uploads directories or folder and here we volition be storing files which will be uploaded.

Storing Files in Multer

At present we will write some server side code to actually store the files which are coming from the client side. Then multer supports different options to store the files we will actually use the simplest one

which is deejay storage approach

Hither in this snippet of code multer is taking use of diskStorage Method inside which we are passing two arguments start argument is destination. Destination is actually where yous demand to store the file. So here we are providing the actual path y'all tin come across cb function 2nd argument is the path. Hither you will write public/uploads and and so we have the 2nd statement which is the actual filename which volition be given to the uploaded file by multer. Then hither you can attach a random name or whatsoever sort of name that y'all want. Here we are providing a random name with the help of Date function.

And lastly yous can see nosotros are calling the multer constructor function passing the option which is storage to the actual storage object which we have defined before on.

So now we need to add some lawmaking to our index.js file and update the lawmaking like this

So at present we demand to distinguish which class field we are processing in multer so for doing this we have a special fields role in multer.

Y'all can see in this block of code we are attaching multiple file input fields inside multer upload function passing array of objects. The offset object is equal to the first input field values which include the name of the input field and the maximum no of files allowed to upload.

So in this manner nosotros need to replace the name parameter to the actual names that we accept given in the index.ejs

Going back to index.js file make this modifications and copy paste the lawmaking

You need to replace it with your proper name parameters as shown below.

So now after replacing it we need to modify the alphabetize.js file and re-create paste the below code

And then now as you tin see inside this we are making a mail service request to the /uploadfile route and passing the middleware function which we have created uploadMultiple we are passing it in the route also. Later uploading the files it will return true and prints it in the console that files uploaded. Every bit you tin can run across in the beneath figure.

hartsocktrainsomill1963.blogspot.com

Source: https://codingshiksha.com/javascript/how-to-upload-multiple-files-in-multiple-form-fields-in-node-js-and-express-using-multer-library-full-example-for-beginners/

0 Response to "Multer Multiple File Uploads in One Form"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel