FileSystemDirectoryHandle: entries() 方法

Baseline 已廣泛支援

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

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

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

FileSystemDirectoryHandle 介面的 entries() 方法會返回一個新的非同步迭代器,用於迭代呼叫該方法的 FileSystemDirectoryHandle 中各項的鍵值對。這些鍵值對的形式是 [key, value] 這樣的陣列。

語法

js
entries()

引數

無。

返回值

一個包含 FileSystemDirectoryHandle 中每一項的鍵值對的新非同步迭代器。

異常

NotAllowedError DOMException

如果控制代碼的 PermissionStatus.stateread 模式下不是 'granted',則會丟擲此錯誤。

NotFoundError DOMException

如果當前項未找到,則會丟擲此錯誤。

示例

使用 for await...of 迴圈可以簡化迭代過程。

js
const dirHandle = await window.showDirectoryPicker();

for await (const [key, value] of dirHandle.entries()) {
  console.log({ key, value });
}

規範

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

瀏覽器相容性

另見