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.
FileSystemWritableFileStream.write(data).then(...);
Promise
which returns undefined
This asynchronous function opens the 'Save File' picker, which returns a FileSystemFileHandle
once a file is selected. From which a writable stream is then created using the FileSystemFileHandle.createWritable()
method.
A user defined Blob
is then written to the stream which is subsequently closed.
async function saveFile() {
const newHandle = await window.showSaveFilePicker();
const writableStream = await newHandle.createWritable();
await writableStream.write(imgBlob);
await writableStream.close();
}
The following show different examples of options that can be passed into the write()
method.
writableStream.write(data)
writableStream.write({ type: "write", position: position, data: data })
writableStream.write({ type: "seek", position: position })
writableStream.write({ type: "truncate", size: size })