FileSystemFileHandle: createWritable() method
The createWritable() method of the FileSystemFileHandle interface creates a FileSystemWritableFileStream that can be used to write to a file. The method returns a Promise which resolves to this created stream.
Any changes made through the stream won't be reflected in the file represented by the file handle until the stream has been closed. This is typically implemented by writing data to a temporary file, and only replacing the file represented by file handle with the temporary file when the writable filestream is closed.
Syntax
createWritable()
createWritable(options)
Parameters
options Optional
-
An object with the following properties:
keepExistingData Optional
-
A Boolean. Default false. When set to true if the file exists, the existing file is first copied to the temporary file. Otherwise the temporary file starts out empty.
Return value
Exceptions
NotAllowedError DOMException
-
Thrown if the PermissionStatus.state for the handle is not 'granted' in readwrite mode.
Examples
The following asynchronous function writes the given contents to the file handle, and thus to disk.
async function writeFile(fileHandle, contents) {
const writable = await fileHandle.createWritable();
await writable.write(contents);
await writable.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 |
createWritable |
86 |
86 |
111 |
No |
72 |
No |
No |
86 |
111 |
61 |
No |
14.0 |
See also