From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../zh-cn/web/api/filereader/readastext/index.html | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 files/zh-cn/web/api/filereader/readastext/index.html (limited to 'files/zh-cn/web/api/filereader/readastext') diff --git a/files/zh-cn/web/api/filereader/readastext/index.html b/files/zh-cn/web/api/filereader/readastext/index.html new file mode 100644 index 0000000000..dd9618490f --- /dev/null +++ b/files/zh-cn/web/api/filereader/readastext/index.html @@ -0,0 +1,110 @@ +--- +title: FileReader.readAsText() +slug: Web/API/FileReader/readAsText +translation_of: Web/API/FileReader/readAsText +--- +
{{APIRef("File API")}}
+ +
 
+ +

readAsText 方法可以将 Blob 或者 File 对象转根据特殊的编码格式转化为内容(字符串形式)

+ +

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

+ +

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

+ +

当转化完成后, readyState 这个参数就会转换 为 done 即完成态, event("loadend") 挂载的事件会被触发,并可以通过事件返回的形参得到中的 FileReader.result 属性得到转化后的结果

+ +

语法

+ +
 instance of FileReader.readAsText(blob[, encoding]);
+ +

参数

+ +
+
二进制对象
+
Blob类型 或 File类型
+
编码类型  (可选)
+
传入一个字符串类型的编码类型,如缺省,则默认为“utf-8”类型
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("File API", "#FileReader-interface", "FileReader")}}{{Spec2("File API")}}Initial definition
+ +

兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureFirefox (Gecko)ChromeInternet ExplorerOperaSafari
Basic support{{CompatGeckoDesktop("1.9.2")}}[1]710[2]12.02[3]6.0.2
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureFirefox Mobile (Gecko)AndroidIE MobileOpera MobileSafari Mobile
Basic support3231011.56.1
+
+ +

 

+ +

[1] 在 Gecko 2.0 beta 7 (Firefox 4.0 beta 7) 之前,所有 File 类型,都是 Blob类型的一个子集,这已经正确的被纳入到规范里了.在 Gecko 13.0 之前,如果我们错误的传参会导致 FileReader 返回一个FileError对象.而FileError 这个实例已经从FileReader 规范中被移除了.而在最新的FileAPI 草稿中定义的错误处理机制是返回一个 DOMError 对象.

+ +

[2] IE9 采用的是 File API Lab.实现编码格式化

+ +

[3] 在Opera11.1 版本中只是被部分支持

+ +

其他文档 

+ + -- cgit v1.2.3-54-g00ecf