Tải lên tệp nodejs API

Các ứng dụng và trang web dành cho thiết bị di động hiện đại cho phép người dùng tải lên ảnh hồ sơ và các tệp khác. Xử lý tệp tải lên là một yêu cầu phổ biến trong khi xây dựng API REST với Node. js & Express

Trong hướng dẫn này, chúng ta sẽ thảo luận về cách xử lý các tệp tải lên một và nhiều tệp bằng Node. js và Express backend và lưu các tệp đã tải lên trên máy chủ

Cài đặt

Đầu tiên, chúng ta hãy tạo một Node mới. js bằng cách chạy các lệnh bên dưới. Tôi đang sử dụng

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
4 để quản lý gói. Bạn có thể sử dụng
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
0 nếu thích

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
1 hoặc
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
2 bỏ qua phiên tương tác và tạo tệp
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
3 dựa trên giá trị mặc định của bạn

Tiếp theo, chạy lệnh sau để cài đặt các phụ thuộc cần thiết

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 

Đây là những gì mỗi gói trên làm

  • # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    4 - Khung web phổ biến được xây dựng trên Node. js. Chúng tôi sẽ sử dụng nó để phát triển API REST
  • # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    5 - Nút. phần mềm trung gian phân tích cú pháp nội dung yêu cầu js phân tích nội dung yêu cầu đến trước trình xử lý của bạn và cung cấp nó trong thuộc tính
    # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    6. Nói tóm lại, nó đơn giản hóa các yêu cầu gửi đến
  • # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    7 - Một phần mềm trung gian Express khác để kích hoạt các yêu cầu CORS (Chia sẻ tài nguyên gốc chéo)
  • # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    8 - Phần mềm trung gian Simple Express để tải tệp lên. Nó phân tích các yêu cầu của
    # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    9, trích xuất các tệp nếu có và cung cấp chúng trong thuộc tính
    # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    20
  • # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    21 - Nút. js phần mềm trung gian để ghi lại các yêu cầu HTTP
  • # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    22 - Một thư viện JavaScript cung cấp các hàm tiện ích cho mảng, số, đối tượng, chuỗi, v.v.

Tạo máy chủ tốc hành

Sau khi cài đặt các phụ thuộc cần thiết, chúng ta hãy bắt đầu tạo máy chủ Express của mình

mục lục. js

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
2

Đoạn mã trên rất đơn giản. Đầu tiên, nó thiết lập phần mềm trung gian

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
8 để kích hoạt yêu cầu
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
9. Sau đó, phần mềm trung gian Express khác được thêm vào để cho phép Chia sẻ tài nguyên nguồn gốc chéo (CORS), phân tích cú pháp nội dung yêu cầu và ghi nhật ký yêu cầu HTTP. Cuối cùng, nó khởi động máy chủ tại cổng
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
25

Tải lên một tệp

Hãy để chúng tôi tạo lộ trình đầu tiên để cho phép người dùng tải lên ảnh hồ sơ của họ

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
9

Đoạn mã trên là một hàm HTTP

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
26. Khi bạn gửi yêu cầu
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
9 đến tuyến đường
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
28 để tải tệp lên, chức năng này sẽ lưu tệp vào thư mục
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
29 trên máy chủ

Phần mềm trung gian # run this for npm $ npm install express body-parser cors express-fileupload morgan lodash --save # or using yarn $ yarn add express body-parser cors express-fileupload morgan lodash 8 hoạt động như thế nào?

Nó làm cho các tệp đã tải lên có thể truy cập được từ thuộc tính

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
20. Ví dụ: nếu bạn đang tải lên một tệp có tên là
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
92 và tên trường của bạn là
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
93, bạn có thể truy cập tệp đó qua
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
94. Đối tượng
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
93 sẽ chứa các thông tin sau

  • # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    96 - Tên của tệp đã tải lên i. e.
    # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    92
  • # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    98 - Chức năng chuyển file đi nơi khác trên server
  • # creat a new directory & switch to it
    $ mkdir files-upload && cd files-upload
    
    # run this for npm to create a new app
    $ npm init -y
    
    # run this for yarn to create a new app
    $ yarn init -y 
    
    99 - Loại kịch câm của tệp
  • # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    40 - Kích thước của tệp tính bằng byte
  • # run this for npm
    $ npm install express body-parser cors express-fileupload morgan lodash --save
    
    # or using yarn
    $ yarn add express body-parser cors express-fileupload morgan lodash 
    
    41 - Biểu diễn bộ đệm của tệp đã tải lên

Tải lên nhiều tệp

Hãy để chúng tôi bắt đầu tạo một lộ trình khác để cho phép người dùng tải lên nhiều ảnh trong một yêu cầu

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
4

Đoạn mã trên tương đối giống với một tệp tải lên, ngoại trừ việc chúng tôi hiện nhận được trường

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
42 thay vì
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
93. Chúng tôi đã sử dụng các hàm tiện ích
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
22 (
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
45 &
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
46) để lặp qua trường
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
42 và lưu từng ảnh vào thư mục
# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
29

Kiểm tra ứng dụng

Chúng ta gần xong rồi. Chạy lệnh sau trong thiết bị đầu cuối của bạn từ thư mục gốc của dự án để khởi động ứng dụng

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
4

Nó sẽ bắt đầu ứng dụng tại cổng

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
25. Hãy để chúng tôi sử dụng Postman để gửi các yêu cầu HTTP
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
9

1. Tệp đơn

Upload Single File

2. Nhiều tệp

Upload Multiple Files

Nếu bạn muốn làm cho các tệp đã tải lên có thể truy cập công khai từ mọi nơi, chỉ cần đặt tĩnh thư mục

# creat a new directory & switch to it
$ mkdir files-upload && cd files-upload

# run this for npm to create a new app
$ npm init -y

# run this for yarn to create a new app
$ yarn init -y 
29

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
8

Bây giờ, bạn có thể mở tệp đã tải lên trực tiếp trong trình duyệt

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
9

Giới hạn kích thước tệp

Nếu bạn muốn giới hạn kích thước của các tệp được tải lên cùng một lúc, hãy chuyển trực tiếp các tùy chọn

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
42 cho phần mềm trung gian
# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
8

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
2

Mã nguồn. Tải xuống mã nguồn hoàn chỉnh từ GitHub có sẵn theo giấy phép MIT

Phần kết luận

Đó là tất cả mọi người. Chúng tôi tìm hiểu cách tải lên một tệp và nhiều tệp bằng Node. js và khung Express. Thư viện

# run this for npm
$ npm install express body-parser cors express-fileupload morgan lodash --save

# or using yarn
$ yarn add express body-parser cors express-fileupload morgan lodash 
8 là một phần mềm trung gian Express dễ sử dụng để xử lý (các) tệp tải lên. Đọc tài liệu của nó để biết thêm các tùy chọn cấu hình

Làm cách nào để tải tệp lên bằng API REST trong nút js?

Nút. js Express Rest API để tải tệp lên
Thiết lập nút. Dự án tải lên tệp js Express
Tạo phần mềm trung gian để tải tệp lên
Hạn chế kích thước tệp với Multer
Tạo Trình điều khiển để tải lên/tải xuống tệp
Xử lý lỗi giới hạn kích thước tệp Multer

Chúng tôi có thể tải tệp lên bằng API không?

API Drive cho phép bạn tải dữ liệu tệp lên khi tạo hoặc cập nhật Tệp . Để biết thông tin về cách tạo Tệp chỉ siêu dữ liệu, hãy tham khảo Tạo tệp.

Làm cách nào để tải tệp lên trong Express js?

Mở trang địa phương http. //127. 0. 0. 1. 2000/ để tải ảnh lên. Chọn hình ảnh để tải lên và nhấp vào nút "Tải hình ảnh lên" . Tại đây, bạn thấy rằng tệp đã được tải lên thành công. Bạn có thể xem tệp đã tải lên trong thư mục "Tải lên".

Loại tệp nào có thể được tải lên trong Nodejs?

Multer là một nút. js được sử dụng để xử lý dữ liệu nhiều phần/biểu mẫu, thư viện được sử dụng chủ yếu để tải tệp lên. Ghi chú. Multer sẽ chỉ xử lý những biểu mẫu có nhiều phần (multipart/form-data). Vì vậy, bất cứ khi nào bạn sử dụng multer, hãy đảm bảo rằng bạn đặt nhiều phần ở dạng