FileSystemSyncAccessHandle: truncate() 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 truncate() của giao diện FileSystemSyncAccessHandle thay đổi kích thước tệp liên kết với handle thành số byte được chỉ định.
Cú pháp
truncate(newSize)
Tham số
newSize-
Số byte để thay đổi kích thước tệp thành.
Giá trị trả về
Không có (undefined).
Ngoại lệ
InvalidStateErrorDOMException-
Được ném ra nếu access handle liên kết đã bị đóng, hoặc nếu việc sửa đổi dữ liệu nhị phân của tệp thất bại.
QuotaExceededError-
Được ném ra nếu
newSizelớn hơn kích thước ban đầu của tệp và vượt quá hạn ngạch lưu trữ của trình duyệt. TypeError-
Được ném ra nếu hệ thống tệp cơ bản không hỗ trợ đặt kích thước tệp thành kích thước mới.
Ví dụ
async function truncateFile() {
// 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();
// Truncate the file to 0 bytes
accessHandle.truncate(0);
// Persist changes to disk.
accessHandle.flush();
// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
}
Thông số kỹ thuật
| Specification |
|---|
| File System> # api-filesystemsyncaccesshandle-truncate> |