FileSystemDirectoryHandle: resolve() 方法

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上執行。自 2023 年 3 月以來,它已在各種瀏覽器中可用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能在 Web Workers 中可用。

FileSystemDirectoryHandle 介面的 resolve() 方法返回一個 Array,其中包含從父控制代碼到指定子條目的目錄名稱,子條目名稱作為陣列的最後一項。

語法

js
resolve(possibleDescendant)

引數

possibleDescendant

要返回相對路徑的 FileSystemHandle

返回值

一個 Promise,它將解析為一個字串 Array,或者如果 possibleDescendant 不是此 FileSystemDirectoryHandle 的後代,則解析為 null

異常

不會丟擲任何異常。

示例

以下非同步函式使用 resolve() 方法查詢相對於指定目錄控制代碼的選定檔案的路徑。

js
async function returnPathDirectories(directoryHandle) {
  // Get a file handle by showing a file picker:
  const [handle] = await self.showOpenFilePicker();
  if (!handle) {
    // User cancelled, or otherwise failed to open a file.
    return;
  }

  // Check if handle exists inside our directory handle
  const relativePaths = await directoryHandle.resolve(handle);

  if (relativePaths === null) {
    // Not inside directory handle
  } else {
    // relativePath is an array of names, giving the relative path
    for (const name of relativePaths) {
      // log each entry
      console.log(name);
    }
  }
}

規範

規範
檔案系統
# api-filesystemdirectoryhandle-resolve

瀏覽器相容性

另見