Class: GDFileReader

GDFileReader()

GDFileReader is an object that allows one to read a file from the BlackBerry Dynamics secure file system. Implements the GDFileSystem APIs.

Constructor

new GDFileReader()

Properties:
Name Type Description
fileName string File name of a secured file.
readyState integer One of the three states the reader can be in EMPTY, LOADING or DONE.
result string The contents of the file that has been read.
error FileError An object containing errors.
onloadstart function Called when the read starts.
onprogress function Called while reading the file, reports progress (progess.loaded/progress.total). -NOT SUPPORTED
onload function Called when the read has successfully completed.
onerror function Called when the read has been aborted. For instance, by invoking the abort() method.
onloadend function Called when the read has failed.
onabort function Called when the request has completed (either in success or failure).
Source:

Methods

abort()

abort - Aborts reading file.
Source:
Example
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    var path = "file.txt",
        options = {create: true, exclusive: false};

    fileSystem.root.getFile(path, options, function (file) {
        file.file(function (file) {
            var reader = new FileReader();
            reader.readAsText(file);
            reader.abort();
            reader.onabort = function() {
                console.log("Reading was aborted");
            };
        });
    }, null);
}, null);

readAsArrayBuffer(file)

Read file and return data as a binary data.
Parameters:
Name Type Description
file GDFile File object containing file properties
Source:
Example
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    var path = "file.txt",
        options = {create: true, exclusive: false};

    fileSystem.root.getFile(path, options, function (file) {
        file.createWriter(function (writer) {
            writer.onwriteend = function() {
                file.file(function (file) {
                    var reader = new FileReader();
                    reader.readAsArrayBuffer(file);
                    reader.onloadend = function (evt) {
                        console.log(evt.target.result.toString()); // "[object ArrayBuffer]"
                    };
                });
            };
            writer.write("text");
        }, null);
    }, null);
}, null);

readAsBinaryString(file)

Read file and return data as a binary data.
Parameters:
Name Type Description
file GDFile File object containing file properties
Source:
Example
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    var path = "file.txt",
        options = {create: true, exclusive: false};

    fileSystem.root.getFile(path, options, function (file) {
        file.createWriter(function (writer) {
            writer.onwriteend = function() {
                file.file(function (file) {
                    var reader = new FileReader();
                    reader.readAsBinaryString(file);
                    reader.onloadend = function (evt) {
                        console.log(evt.target.result); // "text"
                    };
                });
            };
            writer.write("text");
        }, null);
    }, null);
}, null);

readAsDataURL(file)

Read file and return data as a base64 encoded data url. A data url is of the form: data:[][;base64],
Parameters:
Name Type Description
file GDFile File object containing file properties
Source:
Example
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    var path = "logo.png",
        options = {create: true, exclusive: false},
        contentType = 'image/png',
        b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

    fileSystem.root.getFile(path, options, function (file) {
        file.createWriter(function (writer) {
            function b64toBlob(b64Data, contentType, sliceSize) {
                contentType = contentType || '';
                sliceSize = sliceSize || 512;

                var byteCharacters = atob(b64Data);
                var byteArrays = [];

                for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                    var slice = byteCharacters.slice(offset, offset + sliceSize);

                    var byteNumbers = new Array(slice.length);
                    for (var i = 0; i < slice.length; i++) {
                        byteNumbers[i] = slice.charCodeAt(i);
                    }

                    var byteArray = new Uint8Array(byteNumbers);

                    byteArrays.push(byteArray);
                }

                var blob = new Blob(byteArrays, {type: contentType});
                return blob;
            }
            var blob = b64toBlob(b64Data, contentType);

            writer.seek(writer.length);
            writer.onwriteend = function() {
                file.file(function (file) {
                    var reader = new FileReader();
                    reader.readAsDataURL(file);
                    reader.onloadend = function (evt) {
                        console.log(evt.target.result); // "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
                    };
                });
            };
            writer.write(blob);
        }, null);
    }, null);
}, null);

readAsText(file, encoding)

readAsText - Reads text file.
Parameters:
Name Type Description
file GDFile Function to invoke upon successful completion of the request.
encoding string The encoding to use to encode the file's content. Default is UTF8.
Source:
Example
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    var path = "file.txt",
        options = {create: true, exclusive: false};

    fileSystem.root.getFile(path, options, function (file) {
        file.createWriter(function (writer) {
            writer.onwriteend = function() {
                file.file(function (file) {
                    var reader = new FileReader();
                    reader.readAsText(file);
                    reader.onloadend = function (evt) {
                        console.log(evt.target.result); // "text"
                    };
                });
            };
            writer.write("text");
        }, null);
    }, null);
}, null);