blob: fefda6647accebb132740d06fedc3a1db2be2773 (
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
|
---
title: File.getAsText()
slug: Web/API/File/getAsText
tags:
- DOM
- Files
translation_of: Web/API/File/getAsText
---
<p>{{APIRef("File API") }}{{non-standard_header}}</p>
<p>{{deprecated_header(7.0)}}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>getAsText</code> method provides the file's data interpreted as text using a given encoding.</p>
<div class="note">
<p><strong>Note:</strong> This method is obsolete; you should use the {{domxref("FileReader")}} method {{domxref("FileReader.readAsText()","readAsText()")}} instead.</p>
</div>
<h2 id="Syntaxe">Syntaxe</h2>
<pre>var str = instanceOfFile.getAsText(encoding);</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt>encoding</dt>
<dd>A string indicating the encoding to use for the returned data. If this string is empty, UTF-8 is assumed.</dd>
</dl>
<h3 id="Returns">Returns</h3>
<p>A string containing the file's data interpreted as text in the specified <code>encoding</code>.</p>
<h2 id="Example">Example</h2>
<pre class="brush: js">// fileInput is a HTMLInputElement: <input>
var fileInput = document.getElementById("myfileinput");
// files is a FileList object (similar to NodeList)
var files = fileInput.files;
// object for allowed media types
var accept = {
binary : ["image/png", "image/jpeg"],
text : ["text/plain", "text/css", "application/xml", "text/html"]
};
var file;
for (var i = 0; i < files.length; i++) {
file = files[i];
// if file type could be detected
if (file !== null) {
if (accept.text.indexOf(file.mediaType) > -1) {
// file is of type text, which we accept
// make sure it's encoded as utf-8
var data = file.getAsText("utf-8");
// modify data with string methods
} else if (accept.binary.indexOf(file.mediaType) > -1) {
// binary
}
}
}</pre>
<h2 id="Specification">Specification</h2>
<p>Not part of any specification.</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{domxref("File")}}</li>
<li>{{domxref("FileReader")}}</li>
</ul>
|