FileSystemHandle
Baseline 廣泛可用 *
注意:此功能在 Web Workers 中可用。
FileSystemHandle 介面是 File System API 的一部分,它表示一個檔案或目錄條目。多個控制代碼可以指向同一個條目。在大多數情況下,您不會直接使用 FileSystemHandle,而是使用它的子介面 FileSystemFileHandle 和 FileSystemDirectoryHandle。
基於 FileSystemHandle 的介面
以下是基於 FileSystemHandle 介面的介面列表。
FileSystemFileHandle-
表示一個檔案條目的控制代碼。
FileSystemDirectoryHandle-
提供一個目錄條目的控制代碼。
例項屬性
例項方法
isSameEntry()-
比較兩個控制代碼,判斷它們所關聯的條目(檔案或目錄)是否匹配。
queryPermission()實驗性-
查詢當前控制代碼的當前許可權狀態。
remove()實驗性 非標準-
請求從底層檔案系統中移除控制代碼所代表的條目。
requestPermission()實驗性-
請求檔案控制代碼的讀許可權或讀寫許可權。
示例
型別檢查
下面的程式碼允許使用者從檔案選擇器中選擇一個檔案,然後檢查返回的控制代碼是檔案還是目錄。
js
// store a reference to our file handle
let fileHandle;
async function getFile() {
// open file picker
[fileHandle] = await window.showOpenFilePicker();
if (fileHandle.kind === "file") {
// run file code
} else if (fileHandle.kind === "directory") {
// run directory code
}
}
查詢/請求許可權
以下非同步函式將在使用者已授予檔案控制代碼讀或讀寫許可權時返回 true。如果尚未授予許可權,則會請求許可權。
js
// fileHandle is a FileSystemFileHandle
// withWrite is a boolean set to true if write
async function verifyPermission(fileHandle, withWrite) {
const opts = {};
if (withWrite) {
opts.mode = "readwrite";
}
// Check if we already have permission, if so, return true.
if ((await fileHandle.queryPermission(opts)) === "granted") {
return true;
}
// Request permission to the file, if the user grants permission, return true.
if ((await fileHandle.requestPermission(opts)) === "granted") {
return true;
}
// The user did not grant permission, return false.
return false;
}
比較條目
以下函式將單個條目與條目陣列進行比較,並返回一個新陣列,其中移除了任何匹配的條目。
js
function removeMatches(fileEntry, entriesArr) {
const newArr = entriesArr.filter((entry) => !fileEntry.isSameEntry(entry));
return newArr;
}
規範
| 規範 |
|---|
| 檔案系統 # api-filesystemhandle |
瀏覽器相容性
載入中…