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.
The FileReader
allows basic access to a file.
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:
progress.loaded
/progress.total
. (Function)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
.
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.
Parameters:
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 (error) { console.log(error.code); }; entry.file(win, fail);
Parameters:
file: the file object to read.
encoding: the encoding to use to encode the file's content. Default is UTF8.
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 (error) { console.log(error.code); }; entry.file(win, fail);
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);
<!DOCTYPE html> <html> <head> <title>FileReader Example</title> <script type="text/javascript" charset="utf-8" src="cordova.js"></script> <script type="text/javascript" charset="utf-8"> // Wait for device API libraries to load // 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(error) { console.log(error.code); } </script> </head> <body> <h1>Example</h1> <p>Read File</p> </body> </html>
Currently supported on iOS and Android only.
Parameters:
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 (error) { console.log(error.code); }; entry.file(win, fail);
Currently supported on iOS and Android only.
Parameters:
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 (error) { console.log(error.code); }; entry.file(win, fail);