If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
phdesign
Posts: 8
Registered: ‎12-16-2012
My Device: PlayBook
My Carrier: Virgin Mobile
Accepted Solution

Using HTML5 FIleReader / FileWriter on BB10 dev alpha

[ Edited ]

Is this supported? Apart from PhoneGap and w3c examples I can't find any information about the webworks implementation of it and despite my best efforts I can't get it working correctly. I can list files and writing files seems to work (e.g. doesn't error) but I can't read them again. My code for reading files:

 

function readFile(filePath, success, error, progress) {
    window.requestFileSystem(window.PERSISTENT, 0, function(fs) {
        fs.root.getFile(filePath, {}, 
            function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    success(e.target.result);
                };
                reader.onerror = function(err) {
                    if (err.code == FileError.NOT_FOUND_ERR)
                        success(null);
                    else if (_.isFunction(error))
                        error(getFileReaderError(err));
                };
                reader.readAsText(file);
            },
            function(err) {
                if (err.code == FileError.NOT_FOUND_ERR)
                    success(null); 
                else if (_.isFunction(error))
                    error(getError(err));
            });
    });
}

 

 Can any one point me to some working examples?

Please use plain text.
New Contributor
phdesign
Posts: 8
Registered: ‎12-16-2012
My Device: PlayBook
My Carrier: Virgin Mobile

Re: Using HTML5 FIleReader / FileWriter on BB10 dev alpha

Forget it, please ignore this post. After weeks I've finally realised I was passing a fileEntry to the fileReader, not the file object. Working code below:

 

function readFile(filePath, success, error, progress) {
    window.requestFileSystem(window.PERSISTENT, 0, function(fs) {
        fs.root.getFile(filePath, {}, 
            function(fileEntry) {
                fileEntry.file(
                    function(file) {
                        var reader = new FileReader();
                        reader.onloadend = function(e) {
                            success(e.target.result);
                        };
                        reader.onerror = function(err) {
                            if (err.code == FileError.NOT_FOUND_ERR)
                                success(null);
                            else if (_.isFunction(error))
                                error(getFileReaderError(err));
                        };
                        reader.readAsText(file);
                    },
                    function(err) {
                        if (err.code == FileError.NOT_FOUND_ERR)
                            success(null); 
                        else if (_.isFunction(error))
                            error(getError(err));
                    });
                
            },
            function(err) {
                if (err.code == FileError.NOT_FOUND_ERR)
                    success(null); 
                else if (_.isFunction(error))
                    error(getError(err));
            });
    });
}

 

Please use plain text.