aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/filereader/readastext/index.html
blob: 54ab143d73d6cbdc3e4c50774418f8bb48ea6ce2 (plain)
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
---
title: FileReader.readAsText()
slug: Web/API/FileReader/readAsText
translation_of: Web/API/FileReader/readAsText
---
<div>{{APIRef("File API")}}</div>

<div> </div>

<p><code>readAsText</code> 方法可以将 <a href="/zh-CN/docs/Web/API/Blob">Blob</a> 或者 <a href="/zh-CN/docs/Web/API/File">File</a><a href="/zh-CN/docs/MDN/Doc_status/API/File_API"> </a>对象转根据特殊的编码格式转化为内容(字符串形式)</p>

<p>这个方法是异步的,也就是说,只有当执行完成后才能够查看到结果,如果直接查看是无结果的,并返回undefined</p>

<p>也就是说必须要挂载 实例下的 onload 或 onloadend 的方法处理转化后的结果</p>

<p>当转化完成后, <a href="/en-US/docs/Web/API/FileReader/readyState">readyState</a> 这个参数就会转换 为 done 即完成态, <a href="/en-US/docs/Web/Events/loadend">event("loadend")</a> 挂载的事件会被触发,并可以通过事件返回的形参得到中的 <a href="/zh-CN/docs/Web/API/FileReader/result">FileReader.result</a> 属性得到转化后的结果</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><em> instance of <em>FileReader</em></em>.readAsText(blob[, encoding]);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>二进制对象</code></dt>
 <dd><a href="/zh-CN/docs/Web/API/Blob">Blob</a>类型 或 <a href="/zh-CN/docs/Web/API/File">File</a>类型</dd>
 <dt>编码类型  (可选)</dt>
 <dd>传入一个字符串类型的编码类型,如缺省,则默认为“utf-8”类型</dd>
</dl>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("File API", "#FileReader-interface", "FileReader")}}</td>
   <td>{{Spec2("File API")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="兼容性">兼容性</h2>

{{Compat("api.FileReader.readAsText")}}

<h2 id="其他文档">其他文档 </h2>

<ul>
 <li><a href="/zh-CN/docs/Web/API/FileReader">FileReader</a></li>
</ul>