FileSystemDirectoryHandle: phương thức resolve()

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 available in Web Workers.

Phương thức resolve() của giao diện FileSystemDirectoryHandle trả về một Array các tên thư mục từ handle cha đến mục con được chỉ định, với tên của mục con là phần tử cuối cùng trong mảng.

Cú pháp

js
resolve(possibleDescendant)

Tham số

possibleDescendant

FileSystemHandle mà từ đó trả về đường dẫn tương đối.

Giá trị trả về

Một Promise được thực thi thành một Array các chuỗi, hoặc null nếu possibleDescendant không phải là hậu duệ của FileSystemDirectoryHandle này.

Ngoại lệ

Không có ngoại lệ nào được ném ra.

Ví dụ

Hàm bất đồng bộ sau dùng resolve() để tìm đường dẫn đến một tệp đã chọn, tương đối với một handle thư mục được chỉ định.

js
async function returnPathDirectories(directoryHandle) {
  // Lấy một file handle bằng cách hiển thị bộ chọn tệp:
  const [handle] = await self.showOpenFilePicker();
  if (!handle) {
    // Người dùng đã hủy, hoặc không mở được tệp.
    return;
  }

  // Kiểm tra xem handle có tồn tại bên trong handle thư mục của chúng ta không
  const relativePaths = await directoryHandle.resolve(handle);

  if (relativePaths === null) {
    // Không nằm trong handle thư mục
  } else {
    // relativePath là một mảng các tên, biểu thị đường dẫn tương đối
    for (const name of relativePaths) {
      // ghi log từng mục
      console.log(name);
    }
  }
}

Thông số kỹ thuật

Specification
File System
# api-filesystemdirectoryhandle-resolve

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

Xem thêm