Skip to content

Commit e94be7d

Browse files
authored
Merge pull request #512 from bemself/master
File and FileReader
2 parents b11db99 + 2e7c4d7 commit e94be7d

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

4-binary/04-file/article.md

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# File and FileReader
1+
# 文件(File)和文件读取(FileReader
22

3-
A [File](https://www.w3.org/TR/FileAPI/#dfn-file) object inherits from `Blob` and is extended with filesystem-related capabilities.
3+
文件对象 [File](https://www.w3.org/TR/FileAPI/#dfn-file) 继承自 Blob,并扩展了文件系统相关的功能。
44

5-
There are two ways to obtain it.
5+
获取文件对象的方法有两种。
66

7-
First, there's a constructor, similar to `Blob`:
7+
首先,与 Blob 类似,有构造函数:
88

99
```js
1010
new File(fileParts, fileName, [options])
1111
```
1212

13-
- **`fileParts`** -- is an array of Blob/BufferSource/String value, same as `Blob`.
14-
- **`fileName`** -- file name string.
15-
- **`options`** -- optional object:
16-
- **`lastModified`** -- a timestamp (integer date) of last modification.
13+
- **`fileParts`** -- Blob/BufferSource/String 类型值的数组,同 `Blob`
14+
- **`fileName`** -- 文件名字符串。
15+
- **`options`** -- 可选对象:
16+
- **`lastModified`** -- 上次更新的时间戳(整型日期)。
1717

18-
Second, more often we get a file from `<input type="file">` or drag'n'drop or other browser interfaces. Then the file gets these from OS.
18+
其次,我们经常从 `<input type="file">` 或拖拽或其他浏览器接口来获取。 然后,再从操作系统(OS) 中获取文件。
1919

20-
For instance:
20+
例如:
2121

2222
```html run
2323
<input type="file" onchange="showFile(this)">
@@ -26,50 +26,50 @@ For instance:
2626
function showFile(input) {
2727
let file = input.files[0];
2828
29-
alert(`File name: ${file.name}`); // e.g my.png
30-
alert(`Last modified: ${file.lastModified}`); // e.g 1552830408824
29+
alert(`File name: ${file.name}`); // 例如 my.png
30+
alert(`Last modified: ${file.lastModified}`); // 例如 1552830408824
3131
}
3232
</script>
3333
```
3434

3535
```smart
36-
The input may select multiple files, so `input.files` is an array-like object with them. Here we have only one file, so we just take `input.files[0]`.
36+
输入(input)可以选择多个文件, 因此 `input.files` 是类似数组的对象。 此处我们只有一个文件,因此我们只取 `input.files[0]`
3737
```
3838

39-
## FileReader
39+
## 文件读取(FileReader
4040

41-
[FileReader](https://www.w3.org/TR/FileAPI/#dfn-filereader) is an object with the sole purpose of reading data from `Blob` (and hence `File` too) objects.
41+
文件读取 [FileReader](https://www.w3.org/TR/FileAPI/#dfn-filereader) 是从 Blob(以及 `File` )对象中读取数据的对象。
4242

43-
It delivers the data using events, as reading from disk may take time.
43+
由于从磁盘读取数据可能比较费时间,FileReader 通过事件(events)来传递数据。
4444

45-
The constructor:
45+
构造函数:
4646

4747
```js
48-
let reader = new FileReader(); // no arguments
48+
let reader = new FileReader(); // 无参构造
4949
```
5050

51-
The main methods:
51+
主要方法:
5252

53-
- **`readAsArrayBuffer(blob)`** -- read the data as `ArrayBuffer`
54-
- **`readAsText(blob, [encoding])`** -- read the data as a string (encoding is `utf-8` by default)
55-
- **`readAsDataURL(blob)`** -- encode the data as base64 data url.
56-
- **`abort()`** -- cancel the operation.
53+
- **`readAsArrayBuffer(blob)`** -- 读取数据为 `ArrayBuffer`
54+
- **`readAsText(blob, [encoding])`** -- 读取数据为字符串(默认 `utf-8` 编码)
55+
- **`readAsDataURL(blob)`** -- 将数据编码为 base64 的数据 url
56+
- **`abort()`** -- 取消操作。
5757

58-
As the reading proceeds, there are events:
59-
- `loadstart` -- loading started.
60-
- `progress` -- occurs during reading.
61-
- `load` -- no errors, reading complete.
62-
- `abort` -- `abort()` called.
63-
- `error` -- error has occurred.
64-
- `loadend` -- reading finished with either success or failure.
58+
数据读取期间,有以下事件:
59+
- `loadstart` -- 开始加载。
60+
- `progress` -- 读取过程中出现。
61+
- `load` -- 读取完毕,没有错误。
62+
- `abort` -- 调用 `abort()`
63+
- `error` -- 出现错误。
64+
- `loadend` -- 读取完成,或成功或失败。
6565

66-
When the reading is finished, we can access the result as:
67-
- `reader.result` is the result (if successful)
68-
- `reader.error` is the error (if failed).
66+
读取完成后,我们可以如此访问读取的结果:
67+
- `reader.result` 是结果(如成功)
68+
- `reader.error` 是错误(如失败)。
6969

70-
The most widely used events are for sure `load` and `error`.
70+
用的最广泛的事件无疑是 `load` `error`
7171

72-
Here's an example of reading a file:
72+
以下是读取一个文件的示例:
7373

7474
```html run
7575
<input type="file" onchange="readFile(this)">
@@ -94,35 +94,35 @@ function readFile(input) {
9494
</script>
9595
```
9696

97-
```smart header="`FileReader` for blobs"
98-
As mentioned in the chapter <info:blob>, `FileReader` works for any blobs, not just files.
97+
```smart header="`FileReader` 用于 blobs"
98+
<info:blob> 一章中我们提到,`FileReader` 适用于任何块(blobs),不仅仅适用于文件。
9999

100-
So we can use it to convert a blob to another format:
101-
- `readAsArrayBuffer(blob)` -- to `ArrayBuffer`,
102-
- `readAsText(blob, [encoding])` -- to string (an alternative to `TextDecoder`),
103-
- `readAsDataURL(blob)` -- to base64 data url.
100+
因此我们可以用它将一个 blob 转换为其他格式:
101+
- `readAsArrayBuffer(blob)` -- 转换为 `ArrayBuffer`,
102+
- `readAsText(blob, [encoding])` -- 转换为字符串(`TextDecoder` 的一个可替代方法),
103+
- `readAsDataURL(blob)` -- 转换为 base64 的数据 url
104104
```
105105
106106
107-
```smart header="`FileReaderSync` is available for workers only"
108-
For Web Workers, there also exists a synchronous variant of `FileReader`, called [FileReaderSync](https://www.w3.org/TR/FileAPI/#FileReaderSync).
107+
```smart header="`FileReaderSync` 只适用于 workers "
108+
对于 Web Workers,还有一种同步的 `FileReader` 变体,称为 [FileReaderSync](https://www.w3.org/TR/FileAPI/#FileReaderSync)
109109
110-
Its reading methods `read*` do not generate events, but rather return a result, as regular functions do.
110+
FileReader 的读取方法 `read*` 并不生成事件,而是会和普通函数一样返回一个结果。
111111
112-
That's only inside a Web Worker though, because delays in synchronous calls, that are possible while reading from files, in Web Workers are less important. They do not affect the page.
112+
不过,那只是在 Web Worker 内部,因为在读取文件的时候,同步调用会有延迟,而在 Web Workers 则不是很重要,并不会影响页面。
113113
```
114114

115-
## Summary
115+
## 总结
116116

117-
`File` objects inherit from `Blob`.
117+
`File` 对象继承自 `Blob`
118118

119-
In addition to `Blob` methods and properties, `File` objects also have `fileName` and `lastModified` properties, plus the internal ability to read from filesystem. We usually get `File` objects from user input, like `<input>` or drag'n'drop.
119+
除了 `Blob` 方法和属性,`File` 对象还有 `fileName` `lastModified` 属性,以及从文件系统读取的内部方法。 我们通常从用户输入如 `<input>` 或拖拽(drag'n'drop)来获取 `File` 对象。
120120

121-
`FileReader` objects can read from a file or a blob, in one of three formats:
122-
- String (`readAsText`).
123-
- `ArrayBuffer` (`readAsArrayBuffer`).
124-
- Data url, base-64 encoded (`readAsDataURL`).
121+
`FileReader` 对象可以从文件或 blob 读取以下三种格式:
122+
- 字符串 (`readAsText`)
123+
- `ArrayBuffer` (`readAsArrayBuffer`)
124+
- 数据 urlbase-64 编码(`readAsDataURL`)
125125

126-
In many cases though, we don't have to read the file contents. Just as we did with blobs, we can create a short url with `URL.createObjectURL(file)` and assign it to `<a>` or `<img>`. This way the file can be downloaded or shown up as an image, as a part of canvas etc.
126+
但是,多数情况下,我们不必读取文件内容。正如我们处理 blobs 一样,我们可以通过 `URL.createObjectURL(file)` 创建一个短小的 url,并将其赋给 `<a>` `<img>`。 这样,文件便可以下载或者呈现为图像,作为画布(canvas)等的一部分。
127127

128-
And if we're going to send a `File` over a network, that's also easy, as network API like `XMLHttpRequest` or `fetch` natively accepts `File` objects.
128+
而且,如果我们要通过网络发送一个文件(`File`),也简单,因为网络 API `XMLHttpRequest` `fetch` 本质上都接受 `File` 对象。

0 commit comments

Comments
 (0)