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
Contributor
DigitalMaestro
Posts: 36
Registered: ‎03-07-2012
My Carrier: Bell Canada
Accepted Solution

How to read file returned by File Picker

I'm obviously going daft here but for the life of me I cannot figure out how to read in a text file returned by the File Picker.  I am currently getting the correct full path of the file from the File Picker but where do I go from there?  blackberry.io.file does not seem to exist in Blackberry 10 and I am confused as to how I would utilize the HTML5 File API starting from just a file path returned by the File Picker.

 

Can someone please spit out a quick and dirty example showing me the basics of reading in file data starting from a path returned by File Picker please?

 

Thanks very much,

DM

------------
"The shortest answer is doing." - Jacula Prudentum
My app: Cashalyst
Please use plain text.
BlackBerry Development Advisor (Retired)
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Carrier: Virgin

Re: How to read file returned by File Picker

Sure if you have the path to the file then you are part way there. One thing to note. The file MUST be inside you bar(sandbox) OR you need to add the <rim:smileytongue:ermit>access_shared</rim:smileytongue:ermit>
and then the file can live in the shared folder on device.

 

Here is some code you can use to read a file. I poached it from our docs found here:https://developer.blackberry.com/html5/apis/blackberry.io.html

 

<script type="text/javascript">

function readFile() {
    // un-sandbox file system to access shared folder
    blackberry.io.sandbox = false;

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

    window.requestFileSystem(window.TEMPORARY, 1024 * 1024,
        function (fs) {
             // in order to access the shared folder,
             // config.xml must declare the "access_shared" permission
             // reference file by absolute path since file system is un-sandboxed
             fs.root.getFile(blackberry.io.sharedFolder + '/Documents/log.txt', {create: true},
                 function (fileEntry) {
                     fileEntry.file(function (file) {
                         var reader = new FileReader();

                         reader.onloadend = function (e) {
                             var txtArea = document.createElement('textarea');
                             txtArea.value = this.result;
                             document.body.appendChild(txtArea);
                         };

                         reader.readAsText(file);
                    }, errorHandler);
                 }, errorHandler);
        });
}

function errorHandler(e) {
    var msg = '';

    switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
        msg = 'QUOTA_EXCEEDED_ERR';
        break;
    case FileError.NOT_FOUND_ERR:
        msg = 'NOT_FOUND_ERR';
        break;
    case FileError.SECURITY_ERR:
         msg = 'SECURITY_ERR';
         break;
    case FileError.INVALID_MODIFICATION_ERR:
         msg = 'INVALID_MODIFICATION_ERR';
         break;
    case FileError.INVALID_STATE_ERR:
         msg = 'INVALID_STATE_ERR';
         break;
    default:
         msg = 'Unknown Error';
        break;
    };

    console.log('Error: ' + msg);
}
</script>
@erikjohnzon
erjohnson@blackberry.com
Please use plain text.
Contributor
DigitalMaestro
Posts: 36
Registered: ‎03-07-2012
My Carrier: Bell Canada

Re: How to read file returned by File Picker

Excellent. Thank you very much for this. I obviously need to read the online docs a bit more thoroughly :smileyhappy:

DM
------------
"The shortest answer is doing." - Jacula Prudentum
My app: Cashalyst
Please use plain text.