FileSystemSyncAccessHandle: read() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is only available in Dedicated Web Workers.

Phương thức read() của giao diện FileSystemSyncAccessHandle đọc nội dung của tệp liên kết với handle vào một buffer được chỉ định, tùy chọn tại một offset nhất định.

Cú pháp

js
read(buffer, options)

Tham số

buffer

Một ArrayBuffer hoặc ArrayBufferView (chẳng hạn DataView) đại diện cho buffer mà nội dung tệp sẽ được đọc vào. Lưu ý rằng bạn không thể thao tác trực tiếp nội dung của ArrayBuffer. Thay vào đó, hãy tạo một trong các đối tượng mảng có kiểu như Int8Array hoặc đối tượng DataView đại diện cho buffer ở định dạng cụ thể.

options Optional

Một đối tượng tùy chọn chứa các thuộc tính sau:

at

Một số đại diện cho offset tính bằng byte để đọc tệp từ đó.

Giá trị trả về

Một số đại diện cho số byte được đọc từ tệp.

Ngoại lệ

InvalidStateError DOMException

Được ném ra nếu access handle liên kết đã bị đóng.

TypeError

Được ném ra nếu hệ thống tệp cơ bản không hỗ trợ đọc tệp từ offset tệp được chỉ định.

Ví dụ

Hàm xử lý sự kiện bất đồng bộ dưới đây nằm trong một Web Worker. Khi nhận thông báo từ luồng chính, nó thực hiện đọc tệp:

js
onmessage = async (e) => {
  // Retrieve message sent to work from main script
  const message = e.data;

  // Get handle to draft file
  const root = await navigator.storage.getDirectory();
  const draftHandle = await root.getFileHandle("draft.txt", { create: true });
  // Get sync access handle
  const accessHandle = await draftHandle.createSyncAccessHandle();

  // Get size of the file.
  const fileSize = accessHandle.getSize();
  // Read file content to a buffer.
  const buffer = new DataView(new ArrayBuffer(fileSize));
  const readBuffer = accessHandle.read(buffer, { at: 0 });

  // Write the message to the end of the file.
  const encoder = new TextEncoder();
  const encodedMessage = encoder.encode(message);
  const writeBuffer = accessHandle.write(encodedMessage, { at: readBuffer });

  // Persist changes to disk.
  accessHandle.flush();

  // Always close FileSystemSyncAccessHandle if done.
  accessHandle.close();
};

Note: Trong các phiên bản trước của đặc tả, close(), flush(), getSize(), và truncate() được chỉ định sai là phương thức bất đồng bộ. Tuy nhiên, tất cả các trình duyệt hiện tại hỗ trợ các phương thức này đều triển khai chúng là phương thức đồng bộ.

Thông số kỹ thuật

Specification
File System
# api-filesystemsyncaccesshandle-read

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

Xem thêm