Request: phương thức bytes()

Baseline 2025
Newly available

Since January 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Note: This feature is available in Web Workers.

Phương thức bytes() của giao diện Request đọc body của request và trả về nó dưới dạng một promise giải quyết với một Uint8Array.

Cú pháp

js
bytes()

Tham số

Không có.

Giá trị trả về

Một promise giải quyết với một Uint8Array.

Ngoại lệ

TypeError

Được ném ra vì một trong các lý do sau:

RangeError

Được ném ra nếu có vấn đề khi tạo ArrayBuffer liên kết, ví dụ dữ liệu quá lớn.

Ví dụ

js
const myArray = new Uint8Array(10);

const request = new Request("/myEndpoint", {
  method: "POST",
  body: myArray,
});

request.bytes().then((buffer) => {
  // làm gì đó với buffer được gửi trong request
});

Thông số kỹ thuật

Specification
Fetch
# dom-body-bytes

Tương thích trình duyệt

Xem thêm