blob: dd9618490f430b7358786d5a9ec31c6f0ce03663 (
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
---
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>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Firefox (Gecko)</th>
<th>Chrome</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatGeckoDesktop("1.9.2")}}<sup>[1]</sup></td>
<td>7</td>
<td>10<sup>[2]</sup></td>
<td>12.02<sup>[3]</sup></td>
<td>6.0.2</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Firefox Mobile (Gecko)</th>
<th>Android</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>32</td>
<td>3</td>
<td>10</td>
<td>11.5</td>
<td>6.1</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p>[1] 在 Gecko 2.0 beta 7 (Firefox 4.0 beta 7) 之前,所有 <a href="/zh-CN/docs/Web/API/File">File</a> 类型,都是<a href="/zh-CN/docs/Web/API/Blob"> Blob</a>类型的一个子集,这已经正确的被纳入到规范里了.在 Gecko 13.0 之前,如果我们错误的传参会导致 FileReader 返回一个<a href="/zh-CN/docs/Web/API/FileError">FileError</a>对象.而<a href="/zh-CN/docs/Web/API/FileError">FileError</a> 这个实例已经从FileReader 规范中被移除了.而在最新的FileAPI 草稿中定义的错误处理机制是返回一个 <a href="/en-US/docs/Web/API/DOMError">DOMError</a> 对象.</p>
<p>[2] IE9 采用的是 <a href="http://html5labs.interoperabilitybridges.com/prototypes/fileapi/fileapi/info">File API Lab</a>.实现编码格式化</p>
<p>[3] 在Opera11.1 版本中只是被<a href="http://www.opera.com/docs/specs/presto28/file/">部分支持</a></p>
<h2 id="其他文档">其他文档 </h2>
<ul>
<li><a href="/zh-CN/docs/Web/API/FileReader">FileReader</a></li>
</ul>
|