FileSystemDirectoryHandle.resolve()

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

FileSystemDirectoryHandle インターフェイスの resolve() メソッドは、親ハンドルから指定の子エントリーへのディレクトリー名の Array を返します。指定された子エントリーが配列の最後の要素になります。

構文

js
resolve(possibleDescendant)

引数

possibleDescendant

相対パスを返す FileSystemHandle です。

返値

文字列の Array、または possibleDescendant がこの FileSystemDirectoryHandle の子孫でないときは null で解決する Promise を返します。

例外

例外は投げられません。

以下の非同期関数は、resolve() を用いて、選択されたファイルの指定のディレクトリーハンドルを基準とする相対パスを取得します。

js
async function returnPathDirectories(directoryHandle) {
  // ファイルピッカーを開き、ファイルハンドルを得る
  const [handle] = await self.showOpenFilePicker();
  if (!handle) {
    // ユーザーがキャンセルしたか、ファイルを開くのに失敗した
    return;
  }

  // ハンドルがディレクトリーハンドル内に存在するかを確認する
  const relativePaths = await directoryHandle.resolve(handle);

  if (relativePaths === null) {
    // ディレクトリーハンドル内に存在しない
  } else {
    // relativePath は相対パスを表す名前の配列
    for (const name of relativePaths) {
      // 各エントリーを記録する
      console.log(name);
    }
  }
}

仕様書

Specification
File System Standard
# api-filesystemdirectoryhandle-resolve

ブラウザーの互換性

BCD tables only load in the browser

関連情報