Node.js RestApi File Upload (Download) Multiple Files/Images
TIL
771
Male avatar

loveprogramming viết ngày 01/12/2020

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

https://youtu.be/vhA_pXPWylo

Overview – How to build Node.js RestApi File Upload to MySQL/PostgreSQL

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:

Project Structure

Goal:

Upload Web Page Result

We create 2 functions:

Upload Files

  • Upload Single File
  • Upload Multiple Files

Download Files

  • List out uploaded files
  • Download File

Download Files

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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

30 bài viết.
32 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Bài viết liên quan
White
0 5
fCC: Technical Documentation Page note So I have finished the HTML part of this exercise and I want to come here to lament about the lengthy HTML ...
HungHayHo viết hơn 2 năm trước
0 5
White
4 0
I used Spring boot, Hibernate few times back then at University, I'v started using it again recently. In this (Link), I want to check how Spring J...
Rey viết 2 năm trước
4 0
White
24 1
Toán tử XOR có tính chất: + A XOR A = 0 + 0 XOR A = A Với tính chất này, có thể cài đặt bài toán sau với độ phức tạp O(N) về runtime, và với O(1)...
kiennt viết hơn 4 năm trước
24 1
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
30 bài viết.
32 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!