This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
The write() method of the FileSystemWritableFileStream interface writes content into the file the method is called on, at the current file cursor offset.
No changes are written to the actual file on disk until the stream has been closed. Changes are typically written to a temporary file instead. This method can also be used to seek to a byte point within the stream and truncate to modify the total bytes the file contains.
write(data)
dataCan be one of the following:
ArrayBuffer, TypedArray, DataView, Blob, or string.typeA string that is one of "write", "seek", or "truncate".
dataThe file data to write. Can be an ArrayBuffer, TypedArray, DataView, Blob, or string. This property is required if type is set to "write".
positionThe byte position the current file cursor should move to if type "seek" is used. Can also be set if type is "write", in which case the write will start at the specified position.
sizeA number representing the number of bytes the stream should contain. This property is required if type is set to "truncate".
A Promise that returns undefined.
NotAllowedError DOMException
Thrown if PermissionStatus.state is not granted.
QuotaExceededErrorThrown if the new size of the file is larger than the original size of the file, and exceeds the browser's storage quota.
TypeErrorThrown if data is undefined, or if position or size aren't valid.
The following asynchronous function opens the 'Save File' picker, which returns a FileSystemFileHandle once a file is selected. From this, a writable stream is created using the FileSystemFileHandle.createWritable() method.
A text string is then written to the stream, which is subsequently closed.
async function saveFile() {
try {
// create a new handle
const newHandle = await window.showSaveFilePicker();
// create a FileSystemWritableFileStream to write to
const writableStream = await newHandle.createWritable();
// write our file
await writableStream.write("This is my file content");
// close the file and write the contents to disk.
await writableStream.close();
} catch (err) {
console.error(err.name, err.message);
}
}
The following examples show different options that can be passed into the write() method.
// just pass in the data (no options)
writableStream.write(data);
// writes the data to the stream from the determined position
writableStream.write({ type: "write", position, data });
// updates the current file cursor offset to the position specified
writableStream.write({ type: "seek", position });
// resizes the file to be size bytes long
writableStream.write({ type: "truncate", size });
| Specification |
|---|
| File System> # api-filesystemwritablefilestream-write> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
write |
86 | 86 | 111 | 72 | 26 | 109 | 111 | 74 | 26 | 21.0 | 109 | 26 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/write