6 Replies Latest reply on Jun 23, 2016 6:00 AM by duduche1

    File Reader return result "null"

    duduche1

      Hello,

       

      I have made a program how read a file text on the sdcard root. The problem is the filereader function always return null.

       

      The function I use is :

       

       

      function fnTestModuleQuiz()

      {

        $("idBoxTexte").set("value", "1.0.5" + " -- fnListDirectory = " + cordova.file.externalRootDirectory);

       

        window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,

        function(dir){

        try{

        dir.getFile("user.txt", {create:true, exclusive: false},

        function(fileEntry) {

        try{

        fileEntry.file(

        function (file) {

       

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier INFO = " + JSON.encode(file));

       

        /*

        reader.onloadstart = function (evt)

        {

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier onloadstart = " + JSON.encode(evt) + "\n");

        },

        reader.onprogress = function (evt)

        {

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier onprogress = " + JSON.encode(evt) + "\n");

        },

        reader.onabort = function (evt)

        {

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier onabort = " + JSON.encode(evt) + "\n");

        },

        */

        var reader = new FileReader();

        reader.onloadend = function (evt)

        {

        try{

       

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier (onloadend) JSON = " + JSON.encode(evt) + "\n");

       

        if (evt.target.result !== undefined || evt.target.result !== null) {

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier DATA-0 = " + evt.target.result);

        } else if (evt.target.error !== undefined || evt.target.error !== null) {

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier DATA-1 = " + evt.target.error);

        } else {

        $("idBoxTexte").set("value", $("idBoxTexte").get("value") + "\n" + "Fichier DATA-2 = " + "READER_ONLOADEND_ERR " + READER_ONLOADEND_ERR);

        }

       

        }catch(oErr){

        alert("<br/>Erreur reader(0) : " + oErr);

        }

        };

        reader.readAsText(file);

        },

        function(oErr){

        try{

        alert("<br/>Erreur file(0) : " + oErr);

        }catch(oErr){

        alert("<br/>Erreur file(0) : " + oErr);

        }

        }

        );

        }catch(oErr){

        alert("<br/>Erreur getFile(1) : " + oErr);

        }

        },

        function(oErr){

        try{

        alert("<br/>Erreur getFile(0) : " + oErr);

        }catch(oErr){

        alert("<br/>Erreur getFile(0) : " + oErr);

        }

        }

        );

        }catch(oErr){

        alert("<br/>Erreur resolveLocalFileSystemURL(1) : " + oErr);

        }

        },

        function(err){

        try{

        alert("<br/>Erreur resolveLocalFileSystemURL(0) : " + oErr);

        }catch(oErr){

        alert("<br/>Erreur resolveLocalFileSystemURL(0) : " + oErr);

        }

        }

        );

       

       

      }

       

       

       

      I see the size of the file, but the filereader return "null"

       

       

       

      BUILD :

      PhoneGap (iOS / Android / Windows)
      cli-6.1.0 (4.1.0 / 5.1.1 / 4.3.1)
      PLUGIN :
      PluginSourceVersionInstalledLatestPlatforms
      cordova-plugin-camera  npm*2.2.0n/aandroid,ios,winphone
      cordova-plugin-contacts  npm*2.1.0n/aandroid,ios,winphone
      cordova-plugin-device  npm*1.1.2n/aandroid,ios,winphone
      cordova-plugin-device-motion  npm*1.2.1n/aandroid,ios,winphone
      cordova-plugin-device-orientation  npm*1.0.3n/aandroid,ios,winphone
      cordova-plugin-file  npm*n/an/aandroid,ios,winphone
      cordova-plugin-file-transfer  npm*1.5.1n/aandroid,ios,winphone
      cordova-plugin-geolocation  npm*2.2.0n/aandroid,ios,winphone
      cordova-plugin-media  npm*2.3.0n/aandroid,ios,winphone
      cordova-plugin-media-capture  npm*1.3.0n/aandroid,ios,winphone
      cordova-plugin-network-information  npm*1.2.1n/aandroid,ios,winphone
      cordova-plugin-whitelist  npm*1.2.2n/aandroid,ios,winphone
        • 1. Re: File Reader return result "null"
          kerrishotts Adobe Community Professional

          Can you be more specific as to what variable or property is null and which line the issue is on? Otherwise I'm just guessing as to where the problem lies.

          • 2. Re: File Reader return result "null"
            duduche1 Level 1

            Hi,

            Thank you for your anwser, I Have this return in the application console log :

             

            1.0.5 -- fnListDirectory = file:///storage/emulated/0/

             

            Fichier INFO = {"name":"user.txt","localURL":"cdvfile://localhost/sdcard/user.txt","type":"text/plain"," lastModified":1464113522000,"lastModifiedDate":1464113522000,"size":24030,"start":0,"end": 24030}

             

            Fichier (onloadend) JSON = {"type":"loadend","bubbles":false,"cancelBubble":false,"cancelable":false,"lengthComputab le":false,"loaded":0,"total":0,"target":{"_readyState":2,"_error":{"code":"JSON error"},"_result":null,"_progress":0,"_localURL":"cdvfile://localhost/sdcard/user.txt","_ realReader":{}}}

            Fichier DATA-0 = null

             

            So the file user.txt is create by the Cordova filewriter function, with no problem, only the Cordova filereader function have a problem ...

            If you have an idea ?

            Thank's a lot

            • 3. Re: File Reader return result "null"
              kerrishotts Adobe Community Professional

              Can you share what's in "user.txt"? That is, it's definitely not a blank file, right? (The size leads me to believe that there's data there, but I want to be sure.)

               

              Can you share your JSON.encode() function (or indicate what library you're getting that from)? Normally one would use JSON.stringify(obj). I know that particular function is safe with no side effects, but I can't say the same about JSON.encode().

               

              Also, "onloadend" will be fired whether or not the read completed successfully or not. So you do have to worry about failures in that code, and as best I can tell, evt.target._error has something very odd in it: '{code: "JSON error"}'. I don't find that string in the repository, so I'm unsure as to where it's being generated (also why I'm asking about JSON.encode).

               

              Finally, can you double check the version # of the File plugin you're using? You shared a list of plugins in the original post (THANK YOU!), but for some reason that list doesn't show the version # for the file plugin. The current version is 1.5.1, FYI. Also, please share your config.xml file (minus any identifying info you don't want out there) so we can make sure you're getting the right plugins and such.

              • 4. Re: File Reader return result "null"
                duduche1 Level 1

                Thank you for your answer.

                 

                I use the framework MOOTOOLS, the framework provide a JSON.encode function how do the same job to  JSON.stringify

                So, the user.txt was created by the program with a function : and I save the content in this URL : http://www.duche.org/temp/phonegap/help/user.txt

                 

                I put the config.xml at this URL : http://www.duche.org/temp/phonegap/help/config.xml

                 

                Yes I think is "bizarre", when I check the version of the cordova plugin file, I have "n/a" twice ....

                I have this result in the build log section on phonegap website : http://www.duche.org/temp/phonegap/help/build.txt

                • 5. Re: File Reader return result "null"
                  duduche1 Level 1

                  Hello,

                  I have always the same problem with my code, but I have made some change in the config.xml with the order of the plugins :

                  I have write only this list with this order  :

                   

                  <gap:plugin name="cordova-plugin-file" source="npm" />

                    <gap:plugin name="cordova-plugin-file-transfer" source="npm" />

                    <gap:plugin name="cordova-plugin-device" source="npm" />

                    <gap:plugin name="cordova-plugin-device-motion" source="npm" />

                    <gap:plugin name="cordova-plugin-device-orientation" source="npm" />

                    <gap:plugin name="cordova-plugin-network-information" source="npm" />

                    <gap:plugin name="cordova-plugin-contacts" source="npm" />

                    <gap:plugin name="cordova-plugin-camera" source="npm" />

                    <gap:plugin name="cordova-plugin-media" source="npm" />

                    <gap:plugin name="cordova-plugin-media-capture" source="npm" />

                    <gap:plugin name="cordova-plugin-geolocation" source="npm" />

                    <gap:plugin name="cordova-plugin-whitelist" source="npm" />

                   

                  And now I have no "blank" in the Phonegap Installed Plugins.

                   

                  But the code are not good, I have always a variable at null for the content.

                   

                  So someone have a complete piece of code or full package how are good with the readfile function ?

                   

                  Thanks for the anwser.

                  • 6. Re: File Reader return result "null"
                    duduche1 Level 1

                    Someone have a new idea ? or a complete functionnal package ?

                    Thanks a lot for your answer