Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zh-cn]: add translation for FileReaderSync.readAsText() #20274

Merged
merged 3 commits into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions files/zh-cn/web/api/filereadersync/readastext/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: FileReaderSync:readAsText() 方法
slug: Web/API/FileReaderSync/readAsText
l10n:
sourceCommit: 502e8c3f0be95c6f42afe6a72113b029b290b9e8
---

{{APIRef("File API")}} {{AvailableInWorkers("worker_except_service")}}

{{DOMxRef("FileReaderSync")}} 接口的 **`readAsText()`** 方法允许以同步方式读取 {{DOMxRef("File")}} 或 {{DOMxRef("Blob")}} 对象并将其转换为字符串。此接口仅在 [worker](/zh-CN/docs/Web/API/Worker) 中[可用](/zh-CN/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers),因为它支持同步 I/O,可能导致潜在的阻塞。

## 语法

```js-nolint
readAsText(blob)
readAsText(blob, encoding)
```

### 参数

- `blob`
- : 要读取的 {{DOMxRef("File")}} 或 {{DOMxRef("Blob")}} 对象。
- `encoding` {{optional_inline}}
- : 此可选参数指定要使用的编码(例如 `iso-8859-1` 或 `UTF-8`)。如果不存在,该方法将对其应用检测算法以确定其编码。

### 返回值

表示输入数据的字符串。

### 异常

此方法可能引发以下异常:

- `NotFoundError` {{domxref("DOMException")}}
- : 如果无法找到 DOM {{DOMxRef("File")}} 或 {{DOMxRef("Blob")}} 对象表示的资源,例如因为它已被删除,则抛出该异常。
- `SecurityError` {{domxref("DOMException")}}
- : 如果检测到以下有问题的情况之一,则抛出该异常:
- 资源已被第三方修改;
- 同时执行太多读取;
- 资源指向的文件对于从 Web 上使用来说是不安全的(比如它是系统文件)。
- `NotReadableError` {{domxref("DOMException")}}
- : 如果由于权限问题(例如并发锁)而无法读取资源,则抛出该异常。
- `EncodingError` {{domxref("DOMException")}}
- : 如果资源是 data URL 并且超出每个浏览器定义的限制长度,则抛出该异常。

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- [文件 API](/zh-CN/docs/Web/API/File_API)
- {{DOMxRef("File")}}
- {{DOMxRef("FileReaderSync")}}
- {{DOMxRef("FileReader")}}
- {{ domxref("Blob") }}