FileSystemWritableFileStream: seek() method
The seek()
method of the FileSystemWritableFileStream
interface updates the current file cursor offset to the position (in bytes) specified when calling the method.
Syntax
Parameters
-
position
-
A number specifying the byte position from the beginning of the file.
Return value
A Promise
that returns undefined
.
Exceptions
NotAllowedError
DOMException
-
Returned if PermissionStatus.state
is not granted
.
-
TypeError
-
Returned if position
is not a number or not defined.
Examples
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.
Next, we write to the stream:
- A text string is written to the stream.
- The
seek()
method is used to put the cursor at the start of the stream.
- A second text string is written to the start of the stream, overwriting the first write.
The stream is then closed.
async function saveFile() {
try {
const newHandle = await window.showSaveFilePicker();
const writableStream = await newHandle.createWritable();
await writableStream.write("My first file content");
await writableStream.seek(0);
await writableStream.write("My second file content");
await writableStream.close();
} catch (err) {
console.error(err.name, err.message);
}
}
If you run the above function and then open the resulting file created on disk, you should see the text "My second file content".
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 |
seek |
86 |
86 |
111 |
No |
72 |
No |
No |
No |
111 |
No |
No |
No |
See also