support@codebucket.net

How to upload file in ExpressJs using Multer package

How to upload file in ExpressJs using Multer package

K. M. Shawkat Zamil | October 18, 2022

Hello Developers,

In this demonstration, I will show you how to use multipart/form-data to upload any file in ExpressJs. First, you have to install NodeJs in your working environment. Follow the tutorial: (Tutorial Link)

 

First, create a folder and open the command prompt. Then run the below command: 

 

Then create a file named index.js. Run the below command first.

 

npm init --y

 

After that, run the command to install express js. 

 

npm install express --save

 

First, put the below code into the index.js file.

 

var express = require('express');

app = express();

 

After installing the node, we have to use a package called MulterMulter is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of the busboy for maximum efficiency.

 

NOTE: Multer will not process any form which is not multipart (multipart/form-data).

 

Read More: How to handle multipart form data in ExpressJs

 

For installing Multer, run the below command:

 

npm install --save multer

 

After that write some code in the index.js file. In the first line call express and multer and then initialize.

 

/index.js

var express = require('express');
var multer = require('multer');

app = express();

 

Then we need to call multer.diskStorage(). In the diskStorage(), we need to specify two arguments. One is the destination and the other is the filename. Both of the arguments return functions, which also have three parameters named request, file, and callback. A folder should be created in the root, named uploads to save the file.

 

var express = require('express');
app = express();

var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (request, file, callback) {
        callback(null,'./uploads')
    },
    filename: function(request, file,callback) {
        callback(null, file.originalname);
    }
});

 

After that, call the multer and pass the storage, which we have just created. However, there is also call a method called single(). In the single(), we have to set the key.

 

var express = require('express');
app = express();

var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (request, file, callback) {
        callback(null,'./uploads')
    },
    filename: function(request, file,callback) {
        callback(null, file.originalname);
    }
});

var upload = multer({ storage: storage }).single('myFile');

 

Besides, we have to complete the code to call it in the PostMan software.

 

Then call http://localhost:8000/multipartFileData

 

var express = require('express');
app = express();

var multer = require('multer');

var storage = multer.diskStorage({
    destination: function (request, file, callback) {
        callback(null,'./uploads')
    },
    filename: function(request, file,callback) {
        callback(null, file.originalname);
    }
});

var upload = multer({ storage: storage }).single('myFile');

app.post("/multipartFileData", function name(request, response) {
    upload(request, response, function (error) { 
        if (error) { 
            response.end('File Upload Failed');
        } else { 
            response.end('File Upload Success');
        }
    })
})

app.listen(8000, function () { 
    console.log('Server Run Success');
});

 

After all that, we call the listen() method to set the port. After that run the below command in the command line.

 

node index.js

 

Run the below line in the PostMan software, the end-point represents the parameter created in the app.get("/multipartFileData") method. Pass a file in the myFile key. After getting the success message, see the file in the uploads folder.

 

http://localhost:8000/multipartFileData

 

Hope this might help in the development journey.

 

Read More: Learn some important functions in ExpressJs

 

The output is given below:

 

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.