FileSystemHandle

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.

* Some parts of this feature may have varying levels of support.

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

Note: This feature is available in Web Workers.

Giao diện FileSystemHandle của File System API là một đối tượng đại diện cho một mục nhập file hoặc thư mục. Nhiều handle có thể đại diện cho cùng một mục nhập. Phần lớn thời gian, bạn không làm việc trực tiếp với FileSystemHandle mà với các giao diện con của nó là FileSystemFileHandleFileSystemDirectoryHandle.

Các giao diện dựa trên FileSystemHandle

Dưới đây là danh sách các giao diện dựa trên giao diện FileSystemHandle.

FileSystemFileHandle

Đại diện cho một handle tới một mục nhập file.

FileSystemDirectoryHandle

Cung cấp một handle tới một mục nhập thư mục.

Thuộc tính thể hiện

kind Read only

Trả về kiểu của mục nhập. Giá trị là 'file' nếu mục nhập liên quan là một file, hoặc 'directory' nếu là thư mục.

name Read only

Trả về tên của mục nhập liên quan.

Phương thức thể hiện

isSameEntry()

So sánh hai handle để xem các mục nhập liên quan, dù là file hay thư mục, có khớp nhau hay không.

queryPermission() Experimental

Truy vấn trạng thái quyền hiện tại của handle hiện tại.

remove() Experimental Non-standard

Yêu cầu xóa mục nhập được biểu diễn bởi handle khỏi hệ thống file nền.

requestPermission() Experimental

Yêu cầu quyền đọc hoặc đọc-ghi cho file handle.

Ví dụ

Kiểm tra kiểu

Đoạn mã dưới đây cho phép người dùng chọn một file từ bộ chọn file, rồi kiểm tra xem handle trả về là file hay thư mục:

js
// lưu tham chiếu tới file handle của chúng ta
let fileHandle;

async function getFile() {
  // mở bộ chọn file
  [fileHandle] = await window.showOpenFilePicker();

  if (fileHandle.kind === "file") {
    // chạy mã dành cho file
  } else if (fileHandle.kind === "directory") {
    // chạy mã dành cho thư mục
  }
}

Truy vấn/Yêu cầu quyền

Hàm bất đồng bộ sau trả về true nếu người dùng đã cấp quyền đọc hoặc đọc-ghi cho file handle. Nếu chưa, quyền sẽ được yêu cầu.

js
// fileHandle là một FileSystemFileHandle
// withWrite là một boolean được đặt thành true nếu muốn ghi

async function verifyPermission(fileHandle, withWrite) {
  const opts = {};
  if (withWrite) {
    opts.mode = "readwrite";
  }

  // Kiểm tra xem chúng ta đã có quyền chưa, nếu có thì trả về true.
  if ((await fileHandle.queryPermission(opts)) === "granted") {
    return true;
  }

  // Yêu cầu quyền với file, nếu người dùng cấp quyền thì trả về true.
  if ((await fileHandle.requestPermission(opts)) === "granted") {
    return true;
  }

  // Người dùng không cấp quyền, trả về false.
  return false;
}

So sánh các mục nhập

Hàm sau so sánh một mục nhập đơn lẻ với một mảng các mục nhập, và trả về một mảng mới đã loại bỏ mọi mục nhập khớp.

js
function removeMatches(fileEntry, entriesArr) {
  const newArr = entriesArr.filter((entry) => !fileEntry.isSameEntry(entry));

  return newArr;
}

Thông số kỹ thuật

Specification
File System
# api-filesystemhandle

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

Xem thêm