-
Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
78 lines (52 loc) · 2.69 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
title: FileSystemWritableFileStream
slug: Web/API/FileSystemWritableFileStream
page-type: web-api-interface
browser-compat: api.FileSystemWritableFileStream
---
{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers}}
The **`FileSystemWritableFileStream`** interface of the {{domxref("File System API", "File System API", "", "nocode")}} is a {{domxref('WritableStream')}} object with additional convenience methods, which operates on a single file on disk. The interface is accessed through the {{domxref('FileSystemFileHandle.createWritable()')}} method.
{{InheritanceDiagram}}
## Instance properties
_Inherits properties from its parent, {{DOMxRef("WritableStream")}}._
## Instance methods
_Inherits methods from its parent, {{DOMxRef("WritableStream")}}._
- {{domxref('FileSystemWritableFileStream.write()')}}
- : Writes content into the file the method is called on, at the current file cursor offset.
- {{domxref('FileSystemWritableFileStream.seek()')}}
- : Updates the current file cursor offset to the position (in bytes) specified.
- {{domxref('FileSystemWritableFileStream.truncate()')}}
- : Resizes the file associated with the stream to be the specified size in bytes.
## Examples
The following asynchronous function opens the 'Save File' picker, which returns a {{domxref('FileSystemFileHandle')}} once a file is selected. From this, a writable stream is created using the {{domxref('FileSystemFileHandle.createWritable()')}} method.
A text string is then written to the stream, which is subsequently closed.
```js
async function saveFile() {
// 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();
}
```
The following examples show different options that can be passed into the `write()` method.
```js
// 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 });
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [File System API](/en-US/docs/Web/API/File_System_API)
- [The File System Access API: simplifying access to local files](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access)