Web API —— File(文件)
2024-08-20 星期二在了解文件对象之前,我们需要了解Blob
(Binary Large Object),它表示一个不可变的、原始数据的类文件对象。它主要用来处理文件、图片、视频等二进制数据,以及创建 URL 对象,或者上传文件等。
# Blob的特点
- 不可变:一旦创建了 Blob 对象,其内容是不可变的,无法直接修改。你可以创建新的 Blob 来进行更新。
- 二进制数据:Blob 可以存储文本、二进制数据(如文件内容、图像、音频等),并可以设置 MIME 类型,以便数据能被正确地解析和使用。
- 分块存储:Blob 可以包含多个不同类型的数据片段(Blob、ArrayBuffer、字符串等),这使得它非常灵活。
# 创建一个 Blob
const const blob: Blob
blob = new var Blob: new (blobParts?: BlobPart[], options?: BlobPropertyBag) => Blob
A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob)
`Blob` class is a global reference for `import { Blob } from 'node:node:buffer'`
https://nodejs.org/api/buffer.html#class-blob@sincev18.0.0Blob(['hello world'], { BlobPropertyBag.type?: string | undefined
type: 'text/plain' })
const const uInt8Data: Uint8Array<ArrayBuffer>
uInt8Data = new var Uint8Array: Uint8ArrayConstructor
new (elements: Iterable<number>) => Uint8Array<ArrayBuffer> (+5 overloads)
Uint8Array([1, 2, 3, 4, 5])
const const binary: Blob
binary = new var Blob: new (blobParts?: BlobPart[], options?: BlobPropertyBag) => Blob
A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob)
`Blob` class is a global reference for `import { Blob } from 'node:node:buffer'`
https://nodejs.org/api/buffer.html#class-blob@sincev18.0.0Blob([const uInt8Data: Uint8Array<ArrayBuffer>
uInt8Data], {
BlobPropertyBag.type?: string | undefined
type: 'application/octet-stream',
})
# Blob 的常用方法和属性
size
:返回 Blob 对象的大小(以字节为单位)。type
:返回 Blob 对象的 MIME 类型。slice()
:创建一个新的 Blob 对象,表示原始 Blob 的子集。text()
:异步读取 Blob 对象的内容,并将其作为文本字符串返回。arrayBuffer()
:异步读取 Blob 对象的内容,并返回包含数据的 ArrayBuffer。stream()
:返回一个 ReadableStream 对象,用于逐块读取 Blob 的内容。
const const blob: Blob
blob = new var Blob: new (blobParts?: BlobPart[], options?: BlobPropertyBag) => Blob
A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob)
`Blob` class is a global reference for `import { Blob } from 'node:node:buffer'`
https://nodejs.org/api/buffer.html#class-blob@sincev18.0.0Blob(['hello world'], { BlobPropertyBag.type?: string | undefined
type: 'text/plain' })
// 读取 Blob 的文本内容
const blob: Blob
blob.Blob.text(): Promise<string>
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text)text().Promise<string>.then<void, never>(onfulfilled?: ((value: string) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>
Attaches callbacks for the resolution and/or rejection of the Promise.@paramonfulfilled The callback to execute when the Promise is resolved.@paramonrejected The callback to execute when the Promise is rejected.@returnsA Promise for the completion of which ever callback is executed.then(text: string
text => var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.@sincev0.1.100log(text: string
text))
// 读取 Blob 的 ArrayBuffer
const blob: Blob
blob.Blob.arrayBuffer(): Promise<ArrayBuffer>
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/arrayBuffer)arrayBuffer().Promise<ArrayBuffer>.then<void, never>(onfulfilled?: ((value: ArrayBuffer) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>
Attaches callbacks for the resolution and/or rejection of the Promise.@paramonfulfilled The callback to execute when the Promise is resolved.@paramonrejected The callback to execute when the Promise is rejected.@returnsA Promise for the completion of which ever callback is executed.then(arrayBuffer: ArrayBuffer
arrayBuffer => var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.@sincev0.1.100log(arrayBuffer: ArrayBuffer
arrayBuffer))
// 读取 Blob 的二进制数据
const blob: Blob
blob.Blob.stream(): ReadableStream<Uint8Array>
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/stream)stream().ReadableStream<Uint8Array<ArrayBufferLike>>.getReader(): ReadableStreamDefaultReader<Uint8Array<ArrayBufferLike>> (+2 overloads)
[MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/getReader)getReader().ReadableStreamDefaultReader<Uint8Array<ArrayBufferLike>>.read(): Promise<ReadableStreamReadResult<Uint8Array<ArrayBufferLike>>>
[MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/read)read().Promise<ReadableStreamReadResult<Uint8Array<ArrayBufferLike>>>.then<void, never>(onfulfilled?: ((value: ReadableStreamReadResult<Uint8Array<ArrayBufferLike>>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | ... 1 more ... | undefined): Promise<...>
Attaches callbacks for the resolution and/or rejection of the Promise.@paramonfulfilled The callback to execute when the Promise is resolved.@paramonrejected The callback to execute when the Promise is rejected.@returnsA Promise for the completion of which ever callback is executed.then(result: ReadableStreamReadResult<Uint8Array<ArrayBufferLike>>
result => var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.@sincev0.1.100log(result: ReadableStreamReadResult<Uint8Array<ArrayBufferLike>>
result))
# Blob 的常见用途
- 上传文件
- 下载文件
- 处理二进制数据
- 处理文本数据
# File 对象
File 对象是 Blob 的子类,用于表示文件,它可以在 Blob 的使用场景中使用。
# 创建一个 File 对象
const const file: File
file = new var File: new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag) => File
Provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)
`File` class is a global reference for `import { File } from 'node:node:buffer'`
https://nodejs.org/api/buffer.html#class-file@sincev20.0.0File(['hello world'], 'hello.txt', { BlobPropertyBag.type?: string | undefined
type: 'text/plain' })
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.@sincev0.1.100log(const file: File
file.File.name: string
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name)name)
# FileReader 对象
FileReader 对象是用于读取文件的对象,它可以用于异步读取文件的内容,但是它不能按照文件系统的路径名读取文件,如果需要按照文件系统的路径名读取文件,可以使用 FileSystem Access API。
FileReader 对象的常用方法和属性:
readAsText()
:读取文件内容并将其作为文本字符串返回。readAsBinaryString()
:读取文件内容并将其作为二进制字符串返回。readAsArrayBuffer()
:读取文件内容并返回包含数据的 ArrayBuffer。readAsDataURL()
:读取文件内容并返回包含数据的 Data URL。result
:返回读取的文件内容。error
:返回读取过程中的错误信息。readyState
:返回当前读取状态,包括 0:未开始,1:正在读取,2:读取完成。
事件:
- abort:当读取过程被中止时触发。
- error:当读取过程中出现错误时触发。
- load:当成功读取完成时触发。
- loadend:当读取完成时触发,无论读取成功还是失败。
- loadstart:当读取开始时触发。
- progess:当读取进度发生变化时触发。
# 读取文件内容
如果需要同步读取文件内容,可以使用
FileReaderSync
对象。
const const file: File
file = new var File: new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag) => File
Provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)
`File` class is a global reference for `import { File } from 'node:node:buffer'`
https://nodejs.org/api/buffer.html#class-file@sincev20.0.0File(['hello world'], 'hello.txt', { BlobPropertyBag.type?: string | undefined
type: 'text/plain' })
const const reader: FileReader
reader = new var FileReader: new () => FileReader
Lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/FileReader)FileReader()
// 读取文件内容
const reader: FileReader
reader.FileReader.readAsText(blob: Blob, encoding?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/FileReader/readAsText)readAsText(const file: File
file)
// 读取文件内容完成打印
const reader: FileReader
reader.FileReader.addEventListener<"load">(type: "load", listener: (this: FileReader, ev: ProgressEvent<FileReader>) => any, options?: boolean | AddEventListenerOptions): void (+1 overload)
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)addEventListener('load', () => {
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)console.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.@sincev0.1.100log(const reader: FileReader
reader.FileReader.result: string | ArrayBuffer | null
[MDN Reference](https://developer.mozilla.org/docs/Web/API/FileReader/result)result)
})
# 读取文件内容(同步)
此特性仅在 Web Worker(不包括 Service Worker)中可用。
const file = new File(['hello world'], 'hello.txt', { type: 'text/plain' })
const reader = new FileReaderSync()
// 读取文件内容
const text = reader.readAsText(file)
console.log(text)
~ cd ../