Logo

dev-resources.site

for different kinds of informations.

Upload image files with Multer

Published at
1/26/2024
Categories
multer
node
express
fileupload
Author
collinsmutai
Categories
4 categories in total
multer
open
node
open
express
open
fileupload
open
Author
12 person written this
collinsmutai
open
Upload image files with Multer

Multer is a nodejs middleware for uploading files in a nodejs application (Express Multer Middleware, n.d.).
To use multer install the package using

npm install multer
Enter fullscreen mode Exit fullscreen mode

Once it is installed, import the package using this command

const multer = require("multer")
Enter fullscreen mode Exit fullscreen mode

Next is configuring the files' storage destination and preferred filenames. A callback function is used to create the filenames for each of the file uploads.

const storage = multer.diskStorage({
  destination: "./images",
  filename: (req, file, cb) => {
    return cb(
      null,
      `${file.fieldname}_${Date.now()}`
    );
  },
});
Enter fullscreen mode Exit fullscreen mode

After configuring the storage location and the filename, add the storage object to a multer instance.

const upload = multer({ storage: storage });
Enter fullscreen mode Exit fullscreen mode

The middleware is then added to the request to process any incoming file from the request body. A post request to the route "/upload" will upload a single image file from a multi-part form field named product and save it inside a folder called images.

app.post("/upload", upload.single("product"), (req, res) => {
  res.json({
  file_info: req.file,
});
Enter fullscreen mode Exit fullscreen mode

To access the file details, we can use the output of the res.file object and return it in json format.

{
  "file_info": {
    "fieldname": "product",
    "originalname": "img3.jpeg",
    "encoding": "7bit",
    "mimetype": "image/jpeg",
    "destination": "./upload/images",
    "filename": "1706247839665_img3.jpeg",
    "path": "upload/images/1706247839665_img3.jpeg",
    "size": 107421
  }
}
Enter fullscreen mode Exit fullscreen mode

This is an easy way to upload files in a nodejs application. With more configurations, you can specify the required file types and so on.
Credits:

multer Article's
30 articles in total
Favicon
Mastering Image Uploads in Node.js: A Beginner-to-Advanced Guide with Multer and Cloudinary
Favicon
Mastering Image Uploads with Multer, Firebase, and Express in Node.js
Favicon
File-Type Validation in Multer is NOT SAFE🙃
Favicon
Simplify File Uploads with @fluidjs/multer-cloudinary in Express.js
Favicon
How to Upload an Excel File in Node.js Using Express and Multer
Favicon
How to Upload a File in nodejs: A step by step guide
Favicon
Understanding the File Upload Middleware with Cloudinary in Node.js
Favicon
OPTICAL CHARACTER RECOGNITION USING NODE JS AND TESSERACT OCR ENGINE
Favicon
Upload image files with Multer
Favicon
Upload File to S3 Using NestJS Application
Favicon
The easiest way of uploading image and audio files together using Multer, Cloudinary, and Node.js.
Favicon
Uploading and Managing Images with Node.js, Multer, and Cloudinary: A Comprehensive Guide
Favicon
Unexpected end of form when sending form data with Postman- Multer
Favicon
How do i use multer's upload.array(fieldname[, maxCount]) for a nested object inside a mongoose subdocument
Favicon
The file melting Multer with NodeJS and Express
Favicon
Error: Multipart: Boundary not found
Favicon
How to validate uploaded files in Node JS
Favicon
Single and multiple images upload and remove from Cloudinary using Node JS, Multer, MongoDB
Favicon
How To Upload Files With Multer Node.js and Express
Favicon
Nodejs Express RestAPI – Upload/Import CSV File to MySQL – using Fast-CSV & Multer
Favicon
Nodejs Express RestAPI – Upload/Import Excel file/data to MongoDB – using Convert-Excel-to-Json + Multer
Favicon
Nodejs Express RestAPI – Upload/Import Excel File to MySQL – using Read-Excel-File & Multer
Favicon
Node JS Resize Image Upload Using Multer Sharp With Example
Favicon
Nodejs Express RestAPI – Upload/Import CSV file/data to MongoDB – using CsvToJson + Multer
Favicon
NodeJS/Express – Upload/Download MultiparFile to MySQL – Multer + Sequelize + JQuery Ajax + Bootstrap
Favicon
NodeJS/Express – RestAPI to Upload Multipart File to MySQL using Multer + Sequelize ORM
Favicon
NodeJS/Express – Upload/Download MultiparFile to MySQL – Multer + Sequelize + JQuery Ajax + Bootstrap
Favicon
Implementing Multer Storage Engine in TypeScript
Favicon
How To Upload Multiple files to Cloudinary in Nodejs using Promise.all
Favicon
Multer - Build RestAPI to upload a MultipartFile to NodeJS/Express

Featured ones: