FileReader and result does not work

1,607 views
Skip to first unread message

zio budda

unread,
Dec 7, 2011, 8:17:55 AM12/7/11
to phon...@googlegroups.com
Hi all. 
I need to read some files on my phonegap's app (for the moment iOS), so I use File API. 
I have tested the demo on http://docs.phonegap.com/en/1.2.0/phonegap_file_file.md.html#FileReader but without success:  reader.result is always "null". But if I modify 

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

in:

function readAsText(file) {
        var result;    /* ______________________HERE ___*/
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as text");
            console.log(evt.target.result);
            result = evt.target.result;  /* ______________________HERE ___*/
            );
        };
        reader.readAsText(file);
        /*for (var k in reader) {
        console.log(k+" = "+reader[k]);
        }
        */
        console.log(result); /* ______________________HERE ___*/
    }

I can see the file's content (it's stored in "result" variable).

Why this ? 

And: in the iOS Emulator my file path url is /Users/ziobudda/Library/Application Support/iPhone Simulator/4.3/Applications/B20807F1-D375-409B-BAF3-26A55D3E7F61/Documents/
But when I will move onto device ? 

M.
--
Michel 'ZioBudda' Morelli                       mic...@ziobudda.net
Consulenza sistemistica in ambito OpenSource.
Sviluppo applicazioni web dinamiche (LAMP+Ajax)
Telefono: +39-3939890025 --  Fax: +39-0291390660

http://www.ziobudda.net                         ICQ: 58351764
http://www.ziobuddalabs.it                      Skype: zio_budda
http://www.ajaxblog.it                          MSN: mic...@ziobuddalabs.it

Becka11y

unread,
Dec 7, 2011, 3:26:45 PM12/7/11
to phonegap
Sorry, I don't have any ideas why the code would work different. I
have successfully read the contents of a file and output the result
via a console.log statement in the onloadend or onload functions.
Note that FileReader.readAsText(file) expects a File object or a full
file path as the input parameter. How are you creating the file that
you are reading?

I'm not sure I understand your second question? On the device the
file path will be /var/mobile/Applications/<app id>/Documents for the
LocalFileSystem.PERSISTENT FileEnty.

-becky

> */Users/ziobudda/Library/Application
> Support/iPhone
> Simulator/4.3/Applications/B20807F1-D375-409B-BAF3-26A55D3E7F61/Documents/*

ziobudda@gmail

unread,
Dec 7, 2011, 3:36:32 PM12/7/11
to phon...@googlegroups.com
Il giorno 07/dic/2011, alle ore 21:26, Becka11y ha scritto:

Sorry, I don't have any ideas why the code would work different.   I
have successfully read the contents of a file and output the result
via a console.log statement in the onloadend or onload functions.
Note that FileReader.readAsText(file) expects a File object or a full
file path as the input parameter.  How are you creating the file that
you are reading?

Hi, can you try to see if your reader.result contains file content, plz. If I don't wrong understand the API doc, reader.result contain the file's content.

And again: how can I use file content's if in this example the only things is write it to the console ? 
And again: how can I specify another file name via parameter ? 




I'm not sure I understand your second question?   On the device the
file path will be /var/mobile/Applications/<app id>/Documents  for the
LocalFileSystem.PERSISTENT FileEnty.

Thanks. But, how can I read a file in my www directory ?

M.


--
Michel 'ZioBudda' Morelli                       mic...@ziobuddalabs.it
Sviluppo applicazioni CMS DRUPAL e web dinamiche (LAMP+Ajax)
0200619074 - 3939890025 (mobile)--  Fax: +39-0291390660
http://www.ziobuddalabs.it                      Skype: zio_budda

Ken OKABE

unread,
Dec 7, 2011, 3:39:07 PM12/7/11
to phon...@googlegroups.com
http://stackoverflow.com/questions/8003976/phonegap-1-1-0-and-ios-5-0-filewriter-does-not-respond

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Reply all
Reply to author
Forward
0 new messages