summaryrefslogtreecommitdiff
path: root/plugins/org.apache.cordova.file/docs/filereader/filereader.md
blob: 25f6ff06b6e603ad38ce01d005e654fdd30fec17 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
---
license: Licensed to the Apache Software Foundation (ASF) under one
         or more contributor license agreements.  See the NOTICE file
         distributed with this work for additional information
         regarding copyright ownership.  The ASF licenses this file
         to you under the Apache License, Version 2.0 (the
         "License"); you may not use this file except in compliance
         with the License.  You may obtain a copy of the License at

           http://www.apache.org/licenses/LICENSE-2.0

         Unless required by applicable law or agreed to in writing,
         software distributed under the License is distributed on an
         "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
         KIND, either express or implied.  See the License for the
         specific language governing permissions and limitations
         under the License.
---

FileReader
==========

The `FileReader` allows basic access to a file.

Properties
----------

- __readyState__: One of the reader's three possible states, either `EMPTY`, `LOADING` or `DONE`.
- __result__: The contents of the file that have been read. _(DOMString)_
- __error__: An object containing errors. _(FileError)_
- __onloadstart__: Called when the read starts. _(Function)_
- __onload__: Called when the read has successfully completed. _(Function)_
- __onabort__: Called when the read has been aborted. For instance, by invoking the `abort()` method. _(Function)_
- __onerror__: Called when the read has failed. _(Function)_
- __onloadend__: Called when the request has completed (either in success or failure).  _(Function)_

__NOTE:__ The following porperty is not supported:

- __onprogress__: Called while reading the file, reporting progress in terms of `progress.loaded`/`progress.total`. _(Function)_

Methods
-------

- __abort__: Aborts reading file.
- __readAsDataURL__: Read file and return data as a base64-encoded data URL.
- __readAsText__: Reads text file.
- __readAsBinaryString__: Reads file as binary and returns a binary string.
- __readAsArrayBuffer__: Reads file as an `ArrayBuffer`.

Details
-------

The `FileReader` object offers a way to read files from the device's
file system.  Files can be read as text or as a base64 data-encoded
string.  Event listeners receive the `loadstart`, `progress`, `load`,
`loadend`, `error`, and `abort` events.

Supported Platforms
-------------------

- Android
- BlackBerry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7 and 8
- Windows 8

Read As Data URL
----------------

__Parameters:__

- __file__: the file object to read.

Quick Example
-------------

    function win(file) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            console.log("read success");
            console.log(evt.target.result);
        };
        reader.readAsDataURL(file);
    };

    var fail = function (evt) {
        console.log(error.code);
    };

    entry.file(win, fail);

Read As Text
------------

__Parameters:__

- __file__: the file object to read.
- __encoding__: the encoding to use to encode the file's content. Default is UTF8.

Quick Example
-------------

    function win(file) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            console.log("read success");
            console.log(evt.target.result);
        };
        reader.readAsText(file);
    };

    var fail = function (evt) {
        console.log(error.code);
    };

    entry.file(win, fail);

Abort Quick Example
-------------------

    function win(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("read success");
            console.log(evt.target.result);
        };
        reader.readAsText(file);
        reader.abort();
    };

    function fail(error) {
        console.log(error.code);
    }

    entry.file(win, fail);

Full Example
------------

    <!DOCTYPE html>
    <html>
      <head>
        <title>FileReader Example</title>

        <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
        <script type="text/javascript" charset="utf-8">

        // Wait for device API libraries to load
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }

        // device APIs are available
        //
        function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }

        function gotFS(fileSystem) {
            fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
        }

        function gotFileEntry(fileEntry) {
            fileEntry.file(gotFile, fail);
        }

        function gotFile(file){
            readDataUrl(file);
            readAsText(file);
        }

        function readDataUrl(file) {
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read as data URL");
                console.log(evt.target.result);
            };
            reader.readAsDataURL(file);
        }

        function readAsText(file) {
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read as text");
                console.log(evt.target.result);
            };
            reader.readAsText(file);
        }

        function fail(evt) {
            console.log(evt.target.error.code);
        }

        </script>
      </head>
      <body>
        <h1>Example</h1>
        <p>Read File</p>
      </body>
    </html>

iOS Quirks
----------
- The __encoding__ parameter is not supported, and UTF8 encoding is always in effect.

Read As Binary String
---------------------

Currently supported on iOS and Android only.

__Parameters:__

- __file__: the file object to read.

Quick Example
-------------

    function win(file) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            console.log("read success");
            console.log(evt.target.result);
        };
        reader.readAsBinaryString(file);
    };

    var fail = function (evt) {
        console.log(error.code);
    };

    entry.file(win, fail);

Read As Array Buffer
--------------------

Currently supported on iOS and Android only.

__Parameters:__

- __file__:  the file object to read.

Quick Example
-------------

    function win(file) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            console.log("read success");
            console.log(new Uint8Array(evt.target.result));
        };
        reader.readAsArrayBuffer(file);
    };

    var fail = function (evt) {
        console.log(error.code);
    };

    entry.file(win, fail);