Đối tượng Blobs có thể đại diện cho dữ liệu không nhất thiết ở định dạng-bản địa JavaScript. Giao diện Để xây dựng một Blob
đại diện cho một blob, là một đối tượng giống như tệp của dữ liệu thô, bất biến; Chúng có thể được đọc dưới dạng dữ liệu văn bản hoặc nhị phân hoặc được chuyển đổi thành ReadableStream
để các phương thức của nó có thể được sử dụng để xử lý dữ liệu.Blob
object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream
so its methods can be used for processing the data.File
dựa trên Blob
, kế thừa chức năng Blob và mở rộng nó để hỗ trợ các tệp trên hệ thống của người dùng.Sử dụng BLOBS
Blob
từ các đối tượng và dữ liệu không phải Blob khác, hãy sử dụng hàm tạo
1. Để tạo một blob chứa một tập hợp con của dữ liệu của Blob khác, hãy sử dụng phương thức
This example creates a typed array containing the ASCII codes for the space
character through the letter Z, then converts it to an object URL. A link to
open that object URL is created. Click the link to see the decoded object URL.
2. Để có được đối tượng
This example creates a typed array containing the ASCII codes for the space
character through the letter Z, then converts it to an object URL. A link to
open that object URL is created. Click the link to see the decoded object URL.
Blob
cho một tệp trên hệ thống tệp của người dùng, hãy xem tài liệu File
.
API Chấp nhận các đối tượng Blob
cũng được liệt kê trong tài liệu File
.
Người xây dựng
This example creates a typed array containing the ASCII codes for the space
character through the letter Z, then converts it to an object URL. A link to
open that object URL is created. Click the link to see the decoded object URL.
1Trả về một đối tượng Blob
mới được tạo có chứa sự kết hợp của tất cả các dữ liệu trong mảng được truyền vào hàm tạo.
Thuộc tính thể hiện
This example creates a typed array containing the ASCII codes for the space
character through the letter Z, then converts it to an object URL. A link to
open that object URL is created. Click the link to see the decoded object URL.
9 Chỉ đọcRead only Kích thước, tính bằng byte, của dữ liệu có trong đối tượng Blob
.
function showViewLiveResultButton[] {
if [window.self !== window.top] {
// Ensure that if our document is in a frame, we get the user
// to first open it in its own tab or window. Otherwise, this
// example won't work.
const p = document.querySelector["p"];
p.textContent = "";
const button = document.createElement["button"];
button.textContent = "View live result of the example code above";
p.append[button];
button.addEventListener["click", [] => window.open[location.href]];
return true;
}
return false;
}
if [!showViewLiveResultButton[]] {
function typedArrayToURL[typedArray, mimeType] {
return URL.createObjectURL[
new Blob[[typedArray.buffer], { type: mimeType }]
];
}
const bytes = new Uint8Array[59];
for [let i = 0; i window.open[location.href]];
return true;
}
return false;
}
if [!showViewLiveResultButton[]] {
function typedArrayToURL[typedArray, mimeType] {
return URL.createObjectURL[
new Blob[[typedArray.buffer], { type: mimeType }]
];
}
const bytes = new Uint8Array[59];
for [let i = 0; i window.open[location.href]];
return true;
}
return false;
}
if [!showViewLiveResultButton[]] {
function typedArrayToURL[typedArray, mimeType] {
return URL.createObjectURL[
new Blob[[typedArray.buffer], { type: mimeType }]
];
}
const bytes = new Uint8Array[59];
for [let i = 0; i {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
1Trả về một lời hứa giải quyết với một chuỗi chứa toàn bộ nội dung của Blob
được hiểu là văn bản UTF-8.
Ví dụ
Tạo ra một đốm màu
Chất xây dựng
This example creates a typed array containing the ASCII codes for the space
character through the letter Z, then converts it to an object URL. A link to
open that object URL is created. Click the link to see the decoded object URL.
1 có thể tạo các đốm màu từ các đối tượng khác. Ví dụ: để xây dựng một BLOB từ chuỗi JSON:const obj = { hello: "world" };
const blob = new Blob[[JSON.stringify[obj, null, 2]], {
type: "application/json",
}];
Tạo một URL đại diện cho nội dung của một mảng được đánh máy
Mã sau đây tạo ra một mảng gõ JavaScript và tạo một Blob
mới chứa dữ liệu của mảng được đánh máy. Sau đó, nó gọi
const reader = new FileReader[];
reader.addEventListener["loadend", [] => {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
5 để chuyển đổi blob thành URL.HTML
This example creates a typed array containing the ASCII codes for the space
character through the letter Z, then converts it to an object URL. A link to
open that object URL is created. Click the link to see the decoded object URL.
JavaScript
Ví dụ, phần chính của mã này là hàm
const reader = new FileReader[];
reader.addEventListener["loadend", [] => {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
6, tạo ra Blob
từ mảng được đánh máy đã cho và trả về URL đối tượng cho nó. Đã chuyển đổi dữ liệu thành URL đối tượng, nó có thể được sử dụng theo một số cách, bao gồm cả giá trị của thuộc tính const reader = new FileReader[];
reader.addEventListener["loadend", [] => {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
9 của phần tử const reader = new FileReader[];
reader.addEventListener["loadend", [] => {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
8 [tất nhiên là giả sử dữ liệu chứa hình ảnh].function showViewLiveResultButton[] {
if [window.self !== window.top] {
// Ensure that if our document is in a frame, we get the user
// to first open it in its own tab or window. Otherwise, this
// example won't work.
const p = document.querySelector["p"];
p.textContent = "";
const button = document.createElement["button"];
button.textContent = "View live result of the example code above";
p.append[button];
button.addEventListener["click", [] => window.open[location.href]];
return true;
}
return false;
}
if [!showViewLiveResultButton[]] {
function typedArrayToURL[typedArray, mimeType] {
return URL.createObjectURL[
new Blob[[typedArray.buffer], { type: mimeType }]
];
}
const bytes = new Uint8Array[59];
for [let i = 0; i {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
Một cách khác để đọc nội dung từ Blob
là sử dụng
const text = await new Response[blob].text[];
4. Mã sau đọc nội dung của Blob
là văn bản:const text = await new Response[blob].text[];
Hoặc bằng cách sử dụng
const reader = new FileReader[];
reader.addEventListener["loadend", [] => {
// reader.result contains the contents of blob as a typed array
}];
reader.readAsArrayBuffer[blob];
1:const text = await blob.text[];
Bằng cách sử dụng các phương thức khác của
const text = await new Response[blob].text[];
1, có thể đọc nội dung của blob dưới dạng chuỗi hoặc URL dữ liệu.Thông số kỹ thuật
Api file # blob phần # blob-section |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt