FileSystemWritableFileStream: seek() method
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.
Phương thức seek() của giao diện FileSystemWritableFileStream 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 khi gọi phương thức.
Cú pháp
seek(position)
Tham số
position-
Một số xác định vị trí byte từ đầu tệp.
Giá trị trả về
Một Promise trả về undefined.
Ngoại lệ
NotAllowedErrorDOMException-
Được ném ra nếu
PermissionStatus.statekhông phải làgranted. TypeError-
Được ném ra nếu
positionkhông phải là số hoặc không được xác định.
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().
Tiếp theo, chúng ta ghi vào luồng:
- Một chuỗi văn bản được ghi vào luồng.
- Phương thức
seek()được sử dụng để đưa con trỏ về đầu luồng. - Một chuỗi văn bản thứ hai được ghi vào đầu luồng, ghi đè lần ghi đầu tiên.
Sau đó luồng được đóng lại.
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("My first file content");
await writableStream.seek(0);
await writableStream.write("My second file content");
// close the file and write the contents to disk.
await writableStream.close();
} catch (err) {
console.error(err.name, err.message);
}
}
Nếu bạn chạy hàm trên và sau đó mở tệp kết quả được tạo trên đĩa, bạn sẽ thấy văn bản "My second file content".
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| File System> # api-filesystemwritablefilestream-seek> |