FileSystemWritableFileStream
Baseline
2025
Newly available
Since September 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or 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.
Giao diện FileSystemWritableFileStream của File System API là đối tượng WritableStream với các phương thức tiện lợi bổ sung, hoạt động trên một tệp duy nhất trên đĩa. Giao diện này được truy cập thông qua phương thức FileSystemFileHandle.createWritable().
Thuộc tính phiên bản
Kế thừa các thuộc tính từ lớp cha, WritableStream.
Phương thức phiên bản
Kế thừa các phương thức từ lớp cha, WritableStream.
FileSystemWritableFileStream.write()-
Ghi nội dung vào tệp mà phương thức được gọi, tại vị trí con trỏ tệp hiện tại.
FileSystemWritableFileStream.seek()-
Cập nhật vị trí con trỏ tệp hiện tại đến vị trí (tính bằng byte) được chỉ định.
FileSystemWritableFileStream.truncate()-
Thay đổi kích thước tệp liên kết với luồng thành kích thước được chỉ định tính bằng byte.
Ví dụ
Hàm bất đồng bộ sau đây mở hộp thoại 'Save File', trả về FileSystemFileHandle khi một tệp được chọn. Từ đó, một luồng ghi được tạo bằng phương thức FileSystemFileHandle.createWritable().
Sau đó, một chuỗi văn bản được ghi vào luồng, và luồng được đóng lại.
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();
}
Các ví dụ sau đây cho thấy các tùy chọn khác nhau có thể được truyền vào phương thức write().
// 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 });
Thông số kỹ thuật
| Specification |
|---|
| File System> # api-filesystemwritablefilestream> |