#express js #file-upload #node js #multer

File upload in Node JS Using Multer

Table Of Content


  1. Install Multer
  2. Configuring Multer
  3. Adding Multer to Route
  4. Validations

File upload in node js is one hell of a job. Not knowing how is makes it a bit more troublesome. File upload is used at a number of places like image upload, excels for bulk upload etc.

Install Multer

npm install --save multer

from here you can get more details about multer.

Configuring Multer

The preferable place to add the multer configuration is in the routes file.

const multer = require("multer")

const upload = multer({
    storage: multer.diskStorage({
        destination: (req, file, cb) => {
            cb(null, "Uploads")
        },
        filename: (req, file, cb) => {
            cb(
                null,
                file.fieldname + "-" + Date.now() + "-" + file.originalname
            )
        }
    }),
    fileFilter: (req, file, cb) => {
        if (
            file.mimetype ===
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        ) {
            cb(null, true)
        } else {
            cb(null, false)
            return cb(new Error("Only .xlsx format allowed!"))
        }
    }
})

Adding Multer to Route

app.post("/api/file-upload/v1", upload.single("file-key"), (req, res) => {
        console.log(req.file)
        res.send("File uploaded successfully!")
    })

Assuming

<form action="/api/file-upload/v1" method="post" enctype="multipart/form-data">
  <input type="file" name="file-key" />
</form>

Validations

all the validation are to be put in fileFilter section as per the requirement. For this case the system only allow excels to be uploaded.

and we are done.

Feel free to comment you suggestions.

Most Viewed Posts


Must Read


January 28, 2022

#AppImage #linux

How to Add AppImage Application to Menu in Ubuntu (Linux)

AppImages are the most easiest to run compared.The challenging task is to make the AppImage accessible globally through he system like an installed application (in the menu)

Reading time: less than a minute
How to Add Laravel Pint to VS Code Formatter
June 24, 2022

#code-quality #vscode #laravel-pint #laravel

How to Add Laravel Pint to VS Code Formatter

I have been using the unsatisfactory formatters from a long time. All of them have one thing but lack other. Laravel Pint is one who has it all, combining it with vscode is like proving you laravel project a super power.

Reading time: 2 minutes
March 24, 2021

#express js #node #node js #sequelize #mysql

How to create model association in Sequelize - express JS / Node JS

In the very initial days of my work in Node JS if faced this issue. Though Sequelize provide a great ORM for MySQL but the association within the models is a bit tricky.

Reading time: 2 minutes
December 14, 2021

#database #javascript #sequelize #npx

Sequelize CLI Commands

list of Sequelize CLI commands which comes handy, while working on a project.

Reading time: 1 minute
How to create a progressive web app (PWA) in laravel 5.* and above
July 07, 2019

#functionality #pwa #javascript #laravel

How to create a progressive web app (PWA) in laravel 5.* and above

The progressive web app is the new trend as well as the need for the present time. I hope before getting in the “HOW” part you know what is exactly a PWA

Reading time: 4 minutes
March 24, 2021

#express js #node #node js #sequelize #mysql

How to create model association in Sequelize - express JS / Node JS

In the very initial days of my work in Node JS if faced this issue. Though Sequelize provide a great ORM for MySQL but the association within the models is a bit tricky.

Reading time: 2 minutes
February 13, 2021

#express js #node #node js #post #forms

How to get form data in post request in Express JS - Node JS

if you are starting new with Node JS, you will probably face this problem, while submitting the form to a post URL.

Reading time: less than a minute
create-express-boilerplate to create new API projects in a secs
October 25, 2021

#express js #npm #deployment #api #node #node js #javascript #npx #boilerplate

create-express-boilerplate to create new API projects in a secs

Initiating a new project is as exciting as frustrating. Going through all the routine work just to get the basic engine started, yaa I know the pain.

Reading time: less than a minute
November 16, 2021

#file-upload #laravel #tricks

File Upload Made Easy in Laravel

as per me the best approach is to put the entire path of the file in database, instead of just putting the name of the uploaded file.

Reading time: less than a minute
February 03, 2021

#express js #api #node #node js #web services

How to get post data in json in express JS APIs - Node JS

Thought it sounds simple and straight forward, this thing killed a lot of my time at the initial time of getting into node JS.

Reading time: less than a minute
mohammed-samgan-khan

Hi, I am Samgan, I know the adds are bit annoying but i hope you understand. Talking about me, I excel at solving complex problems involving logic and step by step breakdown of the problem. Besides, to develop complex algorithms, I specialize in problem framing, systems design, and product development strategy. Sometimes I also enjoy public speaking.