FileSystemHandle: queryPermission() method
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers}}{{SeeCompatTable}}
The **queryPermission()** method of the {{domxref("FileSystemHandle")}} interface queries the current permission state of the current handle.
Syntax
queryPermission(descriptor)Parameters
descriptor{{optional_inline}}- : An object which specifies the permission mode to query for. Options are as follows:
'mode'{{optional_inline}}- : Can be either
'read','write', or'readwrite'.
Return value
A {{jsxref("Promise")}} that resolves with {{domxref('PermissionStatus.state')}} which is one of 'granted', 'denied' or 'prompt'. It may also reject with one of the exceptions below.
If this resolves with "prompt", the website will have to call requestPermission() before any operations on the handle can be done. If this resolves with "denied" any operations will reject. Usually handles returned by the local file system handle factories will initially resolves with "granted" for their read permission state. However, other than through the user revoking permission, a handle retrieved from IndexedDB is also likely to resolves with "prompt".
Exceptions
- {{jsxref("TypeError")}}
- : Thrown if
modeis specified with a value other than
'read', 'write', or 'readwrite'
Examples
The following asynchronous function returns true if user has granted read or readwrite permissions to the file handle. Permission is requested if not.
// 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;
}Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- File System API
- The File System Access API: simplifying access to local files