Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Node.js RestApi File Upload (Download) Multiple Files/Images
In the tutorial, I will introduce how to create a “Node.js RestApi File Upload” (and Node.js Rest Api File Download) Application with Ajax client to upload/download single or multiple files/images to MySQL/PostgreSQL database with Sequelize engine an Multer middleware.
– We use Express framework to create a Nodejs RestAPIs.
– We use Multer middleware for uploading images/files to Nodejs server.
– We use Sequelize ORM to store file data to database MySQL/PostgreSQL.
To do List:
– I draw a full diagram architecture of Nodejs RestAPI Upload Files
– I configure Sequelize ORM and Multer for Uploading files
– I build Nodejs Express RestApi to upload/download files
– I implement fontend with Jquery Ajax RestAPI client.
Youtube Video Guide – Nodejs Upload Files
Overview – How to build Node.js RestApi File Upload to MySQL/PostgreSQL
For handling the uploading file from rest clients to Node.js Express application, we use multer library. And we use Sequelize library to do CRUD operations with MySQL or PostgreSQL database.
Here is the structure of our project:
Goal:
We create 2 functions:
Upload Files
- Upload Single File
- Upload Multiple Files
Download Files
- List out uploaded files
- Download File
Multer
Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of busboy for maximum efficiency.
NOTE: Multer will not process any form which is not multipart (multipart/form-data).
Install multer by cmd:
$ npm install --save multer
Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.
Basic usage example:
Don’t forget the enctype="multipart/form-data" in your form.
let express = require('express')
let multer = require('multer')
let upload = multer({ dest: 'uploads/' })
let app = express()
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the avatar
file
// req.body will hold the text fields, if there were any
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files is array of photos
files
// req.body will contain the text fields, if there were any
})
Tutorial Link
Node.js RestApi File Upload (Download) Multiple Files/Images
Related post
- Angular Nodejs Fullstack CRUD Application with MySQL/PostgreSQL – Angular 10-9-8 HttpClient + Nodejs Express, Sequelize ORM
- Nodejs JWT Authentication Example – Express RestAPIs + JSON Web Token + BCryptjs + Sequelize + MySQL/PostgreSQL
- Nodejs/Express CSV Upload Download to MySQL/PostgreSQL – Multer, Fast-CSV, Json2Csv, Sequelize




