FileSystemHandle: queryPermission() 方法
注意:此功能在 Web Workers 中可用。
FileSystemHandle 介面的 queryPermission() 方法用於查詢當前控制代碼的當前許可權狀態。
語法
js
queryPermission(descriptor)
引數
descriptor可選-
一個指定要查詢的許可權模式的物件。選項如下:
'mode'可選-
可以是
'read'、'write'或'readwrite'。
返回值
一個 Promise,它會解析為 PermissionStatus.state,其值為 'granted'、'denied' 或 'prompt'。它也可能由於以下任一異常而拒絕。
如果解析為 "prompt",則在該控制代碼上執行任何操作之前,網站必須先呼叫 requestPermission()。如果解析為 "denied",則任何操作都會被拒絕。通常,從本地檔案系統控制代碼工廠返回的控制代碼,其讀取許可權狀態最初會解析為 "granted"。但是,除了使用者撤銷許可權外,從 IndexedDB 檢索到的控制代碼也可能解析為 "prompt"。
異常
TypeError-
如果
mode的值不是'read'、'write'或'readwrite',則會丟擲此異常。
示例
以下非同步函式在使用者已授予檔案控制代碼讀取或讀寫許可權時返回 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;
}
規範
| 規範 |
|---|
| 檔案系統訪問 # api-filesystemhandle-querypermission |
瀏覽器相容性
載入中…