Logo

dev-resources.site

for different kinds of informations.

Single and multiple images upload and remove from Cloudinary using Node JS, Multer, MongoDB

Published at
2/23/2022
Categories
node
mongodb
cloudinary
multer
Author
arifulhasantotul
Categories
4 categories in total
node
open
mongodb
open
cloudinary
open
multer
open
Author
16 person written this
arifulhasantotul
open
Single and multiple images upload and remove from Cloudinary using Node JS, Multer, MongoDB

This system is working without using the file system of node. And I'm also using Thunder client (VS Code extension) for sending request, but you can also use Postman.

First create an ordinary Node JS application.

This is my folder structure and package.json file. I will be using these packages to do the work.
Image description

Go to Cloudinary and create a free account. After creating an account, you will find CLOUDINARY_NAME, CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET in your account dashboard.

1# .env file
=> This file will contain all of the secret you don't want to show others
Image description

2# cloudinary.js file
=> Connects your application to cloudinary. Remember to use dotenv
Image description

3# cloudinaryAccountStorage.js file
=> This file will create a seperate folder to store all of your images in Cloudinary.
Image description

4# people.js file
=> People schema and people model for mongoose to store data in DB.
Image description

5# message.js file
=> Message schema and message model for mongoose to store data in DB.
Image description

6# server.js file
=> Every file is connected here. Remember to import and use dotenv.
Image description

7# uploadRouter.js file
=> Main routing file of this application. Send different request to different path.
Image description

8# imageUploader.js file
=> Universal image uploader file. Maximum file size, maximum file number, file type, and validation can control using this function.
Image description

9# avatarUpload.js file
=> Sending params to imageUploader.js for file size, file number and file type with error message. Most of the tutorial uses upload.single(), upload.array(), upload.fields() but I'm using upload.any()() to give you some idea how it works
Image description

10# attachmentUpload.js file
=> Sending params to imageUploader.js for file size, file number and file type with error message. Most of the tutorial uses upload.single(), upload.array(), upload.fields() but I'm using upload.any()() to give you some idea how it works
Image description

11# uploadController.js file
=> Structure and async function name of controller function. This file control upload and remove images from Cloudinary. It also sends json data to database.
Image description

11.1# addUser function of uploadController.js
=> Upload single image to Cloudinary and send public_id, secure_url to database of cloudinary upload.
Image description
Result of addUser function
Image description

11.2# deleteUser function of uploadController.js
=> Remove single image from Cloudinary also remove from database.
Image description
Result of deleteUser function
Image description

11.3# sendMessage function of uploadController.js
=> Upload multiple images to Cloudinary and send public_id, secure_url to database as an object of different cloudinary upload.
Image description
Result of sendMessage function
Image description

11.4# removeMsgAndAttachments function of uploadController.js
=> Remove multiple images from Cloudinary also remove from database.
Image description
Result of removeMsgAndAttachments function
Image description

Git repo of this code.
Thank you all.

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: