files/en-us/web/api/filesystemhandle/requestpermission/index.md
{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers}}{{SeeCompatTable}}
The requestPermission() method of the
{{domxref("FileSystemHandle")}} interface requests read or readwrite permissions for the
file handle.
requestPermission(descriptor)
descriptor {{optional_inline}}
'mode' {{optional_inline}}
'read', 'write', or 'readwrite'.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.
mode is not that of
'read', 'write', or 'readwrite'SecurityError {{domxref("DOMException")}}
Transient user activation is required. The user has to interact with the page or a UI element in order for this feature to work.
The following asynchronous function requests permissions if they have not been granted.
// 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}}
{{Compat}}