-
Notifications
You must be signed in to change notification settings - Fork 22.5k
/
index.md
73 lines (51 loc) · 2.43 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
---
title: "FileSystemSyncAccessHandle: truncate() method"
short-title: truncate()
slug: Web/API/FileSystemSyncAccessHandle/truncate
page-type: web-api-instance-method
browser-compat: api.FileSystemSyncAccessHandle.truncate
---
{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers("dedicated")}}
The **`truncate()`** method of the
{{domxref("FileSystemSyncAccessHandle")}} interface resizes the file associated with the handle to a specified number of bytes.
> [!NOTE]
> In earlier versions of the spec, {{domxref("FileSystemSyncAccessHandle.close()", "close()")}}, {{domxref("FileSystemSyncAccessHandle.flush()", "flush()")}}, {{domxref("FileSystemSyncAccessHandle.getSize()", "getSize()")}}, and `truncate()` were wrongly specified as asynchronous methods, and older versions of some browsers implement them in this way. However, all current browsers that support these methods implement them as synchronous methods.
## Syntax
```js-nolint
truncate(newSize)
```
### Parameters
- `newSize`
- : The number of bytes to resize the file to.
### Return value
None ({{jsxref('undefined')}}).
### Exceptions
- `InvalidStateError` {{domxref("DOMException")}}
- : Thrown if the associated access handle is already closed, or if the modification of the file's binary data otherwise fails.
- `QuotaExceededError` {{domxref("DOMException")}}
- : Thrown if the `newSize` is larger than the original size of the file, and exceeds the browser's [storage quota](/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria).
- {{jsxref("TypeError")}}
- : Thrown if the underlying file system does not support setting the file size to the new size.
## Examples
```js
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();
}
```
## 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)