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