FileSystemFileHandle: createSyncAccessHandle() method
The createSyncAccessHandle() method of the FileSystemFileHandle interface returns a Promise which resolves to a FileSystemSyncAccessHandle object that can be used to synchronously read from and write to a file. The synchronous nature of this method brings performance advantages, but it is only usable inside dedicated Web Workers for files within the origin private file system.
Creating a FileSystemSyncAccessHandle takes an exclusive lock on the file associated with the file handle. This prevents the creation of further FileSystemSyncAccessHandles or FileSystemWritableFileStreams for the file until the existing access handle is closed.
Syntax
Parameters
Return value
Exceptions
InvalidStateError DOMException
-
Thrown if the FileSystemSyncAccessHandle object does not represent a file in the origin private file system.
NoModificationAllowedError DOMException
-
Thrown if the browser is not able to acquire a lock on the file associated with the file handle.
NotAllowedError DOMException
-
Thrown if the permission has not been granted at the API level (i.e. FileSystemHandle.requestPermission is required).
Examples
The following asynchronous event handler function is contained inside a Web Worker. The snippet inside it creates a synchronous file access handle.
onmessage = async (e) => {
const message = e.data;
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle("draft.txt", { create: true });
const accessHandle = await draftHandle.createSyncAccessHandle();
accessHandle.close();
};
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
createSyncAccessHandle |
102 |
102 |
111 |
No |
88 |
15.2 |
109 |
109 |
111 |
74 |
15.2 |
21.0 |
See also