Hướng dẫn what is blob url in javascript? - url blob trong javascript là gì?

Đối tượng 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.

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 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

Để xây dựng một 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

<p>
  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.
p>
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
<p>
  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.
p>
2. Để có được đối tượng 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

<p>
  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.
p>
1

Trả 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

<p>
  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.
p>
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 < 59; i++) {
    bytes[i] = 32 + i;
  }

  const url = typedArrayToURL(bytes, "text/plain");

  const link = document.createElement("a");
  link.href = url;
  link.innerText = "Open the array URL";

  document.body.appendChild(link);
}
1 Chỉ đọcRead only

Một chuỗi chỉ ra loại MIME của dữ liệu có trong Blob. Nếu loại không xác định, chuỗi này trống.

Phương pháp thể hiện

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 < 59; i++) {
    bytes[i] = 32 + i;
  }

  const url = typedArrayToURL(bytes, "text/plain");

  const link = document.createElement("a");
  link.href = url;
  link.innerText = "Open the array URL";

  document.body.appendChild(link);
}
3

Trả về một lời hứa giải quyết với một

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 < 59; i++) {
    bytes[i] = 32 + i;
  }

  const url = typedArrayToURL(bytes, "text/plain");

  const link = document.createElement("a");
  link.href = url;
  link.innerText = "Open the array URL";

  document.body.appendChild(link);
}
4 chứa toàn bộ nội dung của Blob dưới dạng dữ liệu nhị phân.

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 < 59; i++) {
    bytes[i] = 32 + i;
  }

  const url = typedArrayToURL(bytes, "text/plain");

  const link = document.createElement("a");
  link.href = url;
  link.innerText = "Open the array URL";

  document.body.appendChild(link);
}
6

Trả về một đối tượng Blob mới chứa dữ liệu trong phạm vi byte được chỉ định của blob mà nó được gọi.

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 < 59; i++) {
    bytes[i] = 32 + i;
  }

  const url = typedArrayToURL(bytes, "text/plain");

  const link = document.createElement("a");
  link.href = url;
  link.innerText = "Open the array URL";

  document.body.appendChild(link);
}
8

Trả về một ReadableStream có thể được sử dụng để đọc nội dung của Blob.

const reader = new FileReader();
reader.addEventListener("loadend", () => {
  // reader.result contains the contents of blob as a typed array
});
reader.readAsArrayBuffer(blob);
1

Trả 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

<p>
  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.
p>
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

<p>
  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.
p>

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 < 59; i++) {
    bytes[i] = 32 + i;
  }

  const url = typedArrayToURL(bytes, "text/plain");

  const link = document.createElement("a");
  link.href = url;
  link.innerText = "Open the array URL";

  document.body.appendChild(link);
}

Kết quả

Một cách để đọc nội dung từ Blob là sử dụng

const text = await new Response(blob).text();
1. Mã sau đọc nội dung của Blob dưới dạng mảng được đánh máy:

const reader = new FileReader();
reader.addEventListener("loadend", () => {
  // 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

Sự chỉ rõ
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

Xem thêm

Blob là gì trong một url?

Đối tượng 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. Blobs có thể đại diện cho dữ liệu không nhất thiết ở định dạng-bản địa JavaScript.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. Blobs can represent data that isn't necessarily in a JavaScript-native format.

Blob JavaScript là gì?

Một Blob là một tham chiếu mờ đục hoặc xử lý cho một khối dữ liệu.Tên này xuất phát từ cơ sở dữ liệu SQL, trong đó nó có nghĩa là đối tượng lớn nhị phân.Trong JavaScript, Blobs thường đại diện cho dữ liệu nhị phân và chúng có thể lớn, nhưng không bắt buộc: Blob cũng có thể đại diện cho nội dung của một tệp văn bản nhỏ.an opaque reference to, or handle for, a chunk of data. The name comes from SQL databases, where it means “Binary Large Object.” In JavaScript, Blobs often represent binary data, and they can be large, but neither is required: a Blob could also represent the contents of a small text file.

Tại sao chúng ta cần Blob trong JavaScript?

Blobs cho phép bạn xây dựng tệp như đối tượng trên máy khách mà bạn có thể chuyển đến API mong đợi URL thay vì yêu cầu máy chủ cung cấp tệp.allow you to construct file like objects on the client that you can pass to apis that expect urls instead of requiring the server provides the file.
Nhấp chuột phải vào trang web và nhấp vào Kiểm tra trên mạng trong menu.Khi bảng điều khiển Devtools mở ra, nhấp vào ba chấm dọc ở góc trên bên phải và chọn Khăn Undock vào một cửa sổ riêng biệt.Nhấn Ctrl + F trên Windows hoặc CMD + F trên các thiết bị MAC để tìm URL BLOB.Nhập Blob Blob: HTTP, để tìm liên kết cho video.