FileSystemDirectoryHandle: getDirectoryHandle() 方法
注意:此功能在 Web Workers 中可用。
FileSystemDirectoryHandle 介面的 getDirectoryHandle() 方法會返回一個 FileSystemDirectoryHandle,用於表示在呼叫該方法的目錄控制代碼中,名為指定名稱的子目錄。
語法
js
getDirectoryHandle(name)
getDirectoryHandle(name, options)
引數
name-
一個字串,表示您希望檢索的子目錄的
FileSystemHandle.name。 options可選-
一個可選物件,包含檢索到的子目錄的選項。選項如下:
create可選-
一個布林值,預設為
false。如果設定為true,則在找不到目錄時,會建立並返回指定名稱的目錄。
返回值
一個 Promise,它會解析為一個 FileSystemDirectoryHandle。
異常
NotAllowedErrorDOMException-
如果在
create選項設定為true時,控制代碼的PermissionStatus.state不是'granted'模式(readwrite),或者在create選項設定為false時,不是read模式,則會丟擲此錯誤。 TypeError-
如果指定的名稱不是有效字串,或者包含會干擾本地檔案系統的字元,則會丟擲此錯誤。
TypeMismatchErrorDOMException-
如果返回的條目是一個檔案而不是一個目錄,則會丟擲此錯誤。
NotFoundErrorDOMException-
如果當前條目未找到,或者目標目錄不存在且
create選項設定為false,則會丟擲此錯誤。
示例
下面的示例會返回指定名稱的目錄控制代碼,如果目錄不存在則會建立它。
js
const dirName = "directoryToGetName";
// assuming we have a directory handle: 'currentDirHandle'
const subDir = await currentDirHandle.getDirectoryHandle(dirName, {
create: true,
});
規範
| 規範 |
|---|
| 檔案系統 # api-filesystemdirectoryhandle-getdirectoryhandle |
瀏覽器相容性
載入中…