FileSystemDirectoryHandle: getDirectoryHandle() 方法

Baseline 已廣泛支援

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

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

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

FileSystemDirectoryHandle 介面的 getDirectoryHandle() 方法會返回一個 FileSystemDirectoryHandle,用於表示在呼叫該方法的目錄控制代碼中,名為指定名稱的子目錄。

語法

js
getDirectoryHandle(name)
getDirectoryHandle(name, options)

引數

name

一個字串,表示您希望檢索的子目錄的 FileSystemHandle.name

options 可選

一個可選物件,包含檢索到的子目錄的選項。選項如下:

create 可選

一個布林值,預設為 false。如果設定為 true,則在找不到目錄時,會建立並返回指定名稱的目錄。

返回值

一個 Promise,它會解析為一個 FileSystemDirectoryHandle

異常

NotAllowedError DOMException

如果在 create 選項設定為 true 時,控制代碼的 PermissionStatus.state 不是 'granted' 模式(readwrite),或者在 create 選項設定為 false 時,不是 read 模式,則會丟擲此錯誤。

TypeError

如果指定的名稱不是有效字串,或者包含會干擾本地檔案系統的字元,則會丟擲此錯誤。

TypeMismatchError DOMException

如果返回的條目是一個檔案而不是一個目錄,則會丟擲此錯誤。

NotFoundError DOMException

如果當前條目未找到,或者目標目錄不存在且 create 選項設定為 false,則會丟擲此錯誤。

示例

下面的示例會返回指定名稱的目錄控制代碼,如果目錄不存在則會建立它。

js
const dirName = "directoryToGetName";

// assuming we have a directory handle: 'currentDirHandle'
const subDir = await currentDirHandle.getDirectoryHandle(dirName, {
  create: true,
});

規範

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

瀏覽器相容性

另見