aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/file/getastext
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/file/getastext
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/file/getastext')
-rw-r--r--files/zh-cn/web/api/file/getastext/index.html45
1 files changed, 45 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/file/getastext/index.html b/files/zh-cn/web/api/file/getastext/index.html
new file mode 100644
index 0000000000..a71b8f254e
--- /dev/null
+++ b/files/zh-cn/web/api/file/getastext/index.html
@@ -0,0 +1,45 @@
+---
+title: File.getAsText
+slug: Web/API/File/getAsText
+translation_of: Web/API/File/getAsText
+---
+<p>{{APIRef("File API")}}</p>
+
+<h3 id="概述">概述</h3>
+
+<p>按照指定的编码类型将文件内容解析成字符串并返回.</p>
+
+<h3 id="示例">示例</h3>
+
+<pre class="eval">// fileInput是一个HTMLInputElement元素: &lt;input type="file" id="myfileinput" multiple&gt;
+var fileInput = document.getElementById("myfileinput");
+
+// files是一个FileList对象(类似NodeList)
+var files = fileInput.files;
+
+// 一个对象,包含了允许的MIME类型
+var accept = {
+ binary : ["image/png", "image/jpeg"],
+ text : ["text/plain", "text/css", "application/xml", "text/html"]
+};
+
+var file;
+
+for (var i = 0; i &lt; files.length; i++) {
+
+ file = files[i];
+
+ // 如果文件的类型能够被检测到
+ if (file !== null) {
+
+ if (accept.text.indexOf(file.mediaType) &gt; -1) {
+
+ // file是个可接受的文本文件,使用utf-8编码读取
+ var data = file.getAsText("utf-8");
+ // 使用字符串方法处理data
+
+ } else if (accept.binary.indexOf(file.mediaType) &gt; -1) {
+ // file是个可接受的二进制文件
+ }
+ }
+}</pre>