FileSystemHandle: queryPermission() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

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

注意:此功能在 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

瀏覽器相容性

另見