FileSystemSyncAccessHandle: getSize() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is only available in Dedicated Web Workers.
Phương thức getSize() của giao diện FileSystemSyncAccessHandle trả về kích thước của tệp liên kết với handle tính bằng byte.
Note:
Trong các phiên bản trước của đặc tả, close(), flush(), getSize(), và truncate() được chỉ định sai là phương thức bất đồng bộ. Tuy nhiên, tất cả các trình duyệt hiện tại hỗ trợ các phương thức này đều triển khai chúng là phương thức đồng bộ.
Cú pháp
getSize()
Tham số
Không có.
Giá trị trả về
Một số đại diện cho kích thước của tệp tính bằng byte.
Ngoại lệ
InvalidStateErrorDOMException-
Được ném ra nếu access handle liên kết đã bị đóng.
Ví dụ
Hàm xử lý sự kiện bất đồng bộ dưới đây nằm trong một Web Worker. Khi nhận thông báo từ luồng chính, nó tạo một file access handle đồng bộ, lấy kích thước tệp, và thực hiện các thao tác đọc/ghi:
onmessage = async (e) => {
// Retrieve message sent to work from main script
const message = e.data;
// Get handle to draft file
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle("draft.txt", { create: true });
// Get sync access handle
const accessHandle = await draftHandle.createSyncAccessHandle();
// Get size of the file.
const fileSize = accessHandle.getSize();
// Read file content to a buffer.
const buffer = new DataView(new ArrayBuffer(fileSize));
const readBuffer = accessHandle.read(buffer, { at: 0 });
// Write the message to the end of the file.
const encoder = new TextEncoder();
const encodedMessage = encoder.encode(message);
const writeBuffer = accessHandle.write(encodedMessage, { at: readBuffer });
// Persist changes to disk.
accessHandle.flush();
// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
};
Thông số kỹ thuật
| Specification |
|---|
| File System> # api-filesystemsyncaccesshandle-getsize> |