logo hsb.horse
← Back to snippets index

Snippets

File System API Permission Check

Function to check and request permissions for browser File System API. Supports both read-only and read-write modes.

Published: Updated:

Translations

Check and request permissions for browser File System API.

Returns true immediately if current permission is granted. Requests permission if not granted. Permission request must be called within a user click event.

async function verifyPermission(handle: FileSystemHandle, readWrite: boolean) {
const options = {};
if (readWrite) {
options.mode = 'readwrite';
}
// 1. Check current permission
if ((await handle.queryPermission(options)) === 'granted') {
return true;
}
// 2. Request permission if not granted
if ((await handle.requestPermission(options)) === 'granted') {
return true;
}
return false;
}