14 Replies Latest reply on Feb 2, 2017 7:21 AM by ahmadalattar

    how to i create , write and save a file with cordova and phonegap ?

    ahmadalattar

      hello ,

      am new to phonegap , ive downloaded the windows version and mobile server version.

      finished my jquerymobile layout , and now am trying to call cordova <script> and wrote the file create function , with no luck, no error and no success.

       

      can anyone help on that ?

        • 1. Re: how to i create , write and save a file with cordova and phonegap ?
          VectorP Level 4

          Did you:

          - include the file plugin from npm in your config

          - include a reference to cordova.js in your html

          - wait for the deviceready event to fire

          - use the file plugin's interface functions as described in its docs

          ?

          • 2. Re: how to i create , write and save a file with cordova and phonegap ?
            ahmadalattar Level 1

            hello vector ,

            - include the file plugin from npm in your config

            i installed npm and installed cordova-plugin-file , as for the config file :

                <plugin name="cordova-plugin-file-transfer" source="npm" spec="~1.5.0" />

             

            - include a reference to cordova.js in your html

            already done <script src="souceofcordova.js"></script>

             

            - wait for the deviceready event to fire

            yes its ready , BUT am viewing the app via a web browser and mobile view from phonegap server

             

            - use the file plugin's interface functions as described in its docs

            i did that i didnot understand , so i copy paste it , and called the funcion onclick

             

            still i dont see the file created, nor the javascript alert ive added.

            • 3. Re: how to i create , write and save a file with cordova and phonegap ?
              VectorP Level 4

              <script src="souceofcordova.js"></script>

              Really like that?

              Sould be <script src="cordova.js"></script>

               

              am viewing the app via a web browser

              Try testing on a real device. Or rather: all of your test devices.

              • 4. Re: how to i create , write and save a file with cordova and phonegap ?
                ahmadalattar Level 1

                hello ,

                 

                no am explaining as an example.

                as for the view , on a win web browser , and phonegap mobile similator

                 

                the scripts link :

                <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
                <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

                    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

                <script type="text/javascript" src="js/cordova.js"></script>

                    <script type="text/javascript" src="js/index.js"></script>

                 

                the function:

                 

                <script type="text/javascript">

                        app.initialize();

                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {

                 

                 

                    console.log('file system open: ' + fs.name);

                    fs.root.getFile("newFile.txt", { create: true, exclusive: false }, function (fileEntry) {

                 

                 

                        console.log("fileEntry is file?" + fileEntry.isFile.toString());

                        // fileEntry.name == 'someFile.txt'

                        // fileEntry.fullPath == '/someFile.txt'

                        writeFile(fileEntry, null);

                 

                 

                    }, onErrorCreateFile);

                 

                 

                }, onErrorLoadFs);

                 

                 

                function gotFS(fileSystem) {

                alert("hola");

                fileSystem.root.getFile("newFile.txt", {create: true, exclusive: false}, gotFileEntry, fail);

                    }

                 

                 

                    function gotFileEntry(fileEntry) {

                        fileEntry.createWriter(gotFileWriter, fail);

                    }

                 

                 

                    function gotFileWriter(writer) {

                        writer.onwriteend = function(evt) {

                            console.log("contents of file now 'some sample text'");

                            writer.truncate(11);

                            writer.onwriteend = function(evt) {

                                console.log("contents of file now 'some sample'");

                                writer.seek(4);

                                writer.write(" different text");

                                writer.onwriteend = function(evt){

                                    console.log("contents of file now 'some different text'");

                  alert("DONE");

                                }

                            };

                        };

                        writer.write("some sample text");

                    }

                 

                 

                    function fail(error) {

                        console.log(error.code);

                    }

                 

                </script>

                 

                note that : even the alerts wont work to track the problem , and in the developer mode , i keep reading undefined root,fileSystem etc objects.

                • 5. Re: how to i create , write and save a file with cordova and phonegap ?
                  VectorP Level 4

                  <script type="text/javascript" src="js/cordova.js"></script>

                  As I wrote: this is not correct. You should code it as written above in my post.

                  Also, you are not waiting for the deviceready event to fire. Use the callback of the eventListener to wait for that, then attempt to invoke the file plugin.

                  • 6. Re: how to i create , write and save a file with cordova and phonegap ?
                    ahmadalattar Level 1

                    hello ,

                     

                    the same problem occurs , errors :

                     

                    Uncaught ReferenceError: LocalFileSystem is not defined

                    Uncaught TypeError: Cannot read property 'getFile' of undefined

                    • 7. Re: how to i create , write and save a file with cordova and phonegap ?
                      VectorP Level 4

                      Then the plugin is not active. Several possibilities:

                      - your config is incorrect (or you changed it while Hydration is switched on)

                      - your plugin reference is incorrect

                      - you are building locally and merely included the plugin in config, without installing the plugin in your project

                      - your reference of cordova.js is still incorrect

                      - you are still not waiting for the deviceready event to fire

                      - you are not testing on a real device

                       

                      Since I can't see which of the above is causing your trouble, it's hard for me to tell for sure what's going on.

                      • 8. Re: how to i create , write and save a file with cordova and phonegap ?
                        ahmadalattar Level 1

                        hello ,

                        some of what you are saying is currect , am not using/testing it on a real mobile , its locally with my windows browser and mobile phonegap server app.

                         

                        my refrence of cordova is currect.

                        deviceready status ok

                        as for my config file , i dont know how to config it.

                        my config file :

                        <?xml version='1.0' encoding='utf-8'?>

                        <widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">

                            <name>helloworld</name>

                            <description>

                                Hello World sample application that responds to the deviceready event.

                            </description>

                            <author email="support@phonegap.com" href="http://phonegap.com">

                                PhoneGap Team

                            </author>

                            <content src="index.html" />

                            <preference name="DisallowOverscroll" value="true" />

                            <preference name="android-minSdkVersion" value="14" />

                          <preference name="AndroidPersistentFileLocation" value="Internal" />

                          <preference name="iosPersistentFileLocation" value="Library" />

                        <preference name="iosPersistentFileLocation" value="Compatibility" />

                        <preference name="AndroidPersistentFileLocation" value="Compatibility" />

                        <preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />

                        <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />

                            <plugin name="cordova-plugin-battery-status" source="npm" spec="~1.1.1" />

                            <plugin name="cordova-plugin-camera" source="npm" spec="~2.1.1" />

                            <plugin name="cordova-plugin-media-capture" source="npm" spec="~1.2.0" />

                            <plugin name="cordova-plugin-console" source="npm" spec="~1.0.2" />

                            <plugin name="cordova-plugin-contacts" source="npm" spec="~2.0.1" />

                            <plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />

                            <plugin name="cordova-plugin-device-motion" source="npm" spec="~1.2.0" />

                            <plugin name="cordova-plugin-device-orientation" source="npm" spec="~1.0.2" />

                            <plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.0" />

                            <plugin name="cordova-plugin-file" source="npm" spec="~4.1.1" />

                            <plugin name="cordova-plugin-file-transfer" source="npm" spec="~1.5.0" />

                            <plugin name="cordova-plugin-geolocation" source="npm" spec="~2.1.0" />

                            <plugin name="cordova-plugin-globalization" source="npm" spec="~1.0.3" />

                            <plugin name="cordova-plugin-inappbrowser" source="npm" spec="~1.3.0" />

                            <plugin name="cordova-plugin-media" source="npm" spec="~2.2.0" />

                            <plugin name="cordova-plugin-network-information" source="npm" spec="~1.2.0" />

                            <plugin name="cordova-plugin-splashscreen" source="npm" spec="~3.2.1" />

                            <plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.2" />

                            <plugin name="cordova-plugin-vibration" source="npm" spec="~2.1.0" />

                            <plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />

                            <platform name="android">

                                <icon density="ldpi" src="www/res/icon/android/drawable-ldpi-icon.png" />

                                <icon density="mdpi" src="www/res/icon/android/drawable-mdpi-icon.png" />

                                <icon density="hdpi" src="www/res/icon/android/drawable-hdpi-icon.png" />

                                <icon density="xhdpi" src="www/res/icon/android/drawable-xhdpi-icon.png" />

                                <icon density="xxhdpi" src="www/res/icon/android/drawable-xxhdpi-icon.png" />

                                <icon density="xxxhdpi" src="www/res/icon/android/drawable-xxxhdpi-icon.png" />

                                <splash density="land-ldpi" src="www/res/screen/android/drawable-land-ldpi-screen.png" />

                                <splash density="land-mdpi" src="www/res/screen/android/drawable-land-mdpi-screen.png" />

                                <splash density="land-hdpi" src="www/res/screen/android/drawable-land-hdpi-screen.png" />

                                <splash density="land-xhdpi" src="www/res/screen/android/drawable-land-xhdpi-screen.png" />

                                <splash density="land-xxhdpi" src="www/res/screen/android/drawable-land-xxhdpi-screen.png" />

                                <splash density="land-xxxhdpi" src="www/res/screen/android/drawable-land-xxxhdpi-screen.png" />

                                <splash density="port-ldpi" src="www/res/screen/android/drawable-port-ldpi-screen.png" />

                                <splash density="port-mdpi" src="www/res/screen/android/drawable-port-mdpi-screen.png" />

                                <splash density="port-hdpi" src="www/res/screen/android/drawable-port-hdpi-screen.png" />

                                <splash density="port-xhdpi" src="www/res/screen/android/drawable-port-xhdpi-screen.png" />

                                <splash density="port-xxhdpi" src="www/res/screen/android/drawable-port-xxhdpi-screen.png" />

                                <splash density="port-xxxhdpi" src="www/res/screen/android/drawable-port-xxxhdpi-screen.png" />

                            </platform>

                            <platform name="ios">

                                <icon height="57" platform="ios" src="www/res/icon/ios/icon.png" width="57" />

                                <icon height="114" platform="ios" src="www/res/icon/ios/icon@2x.png" width="114" />

                                <icon height="40" platform="ios" src="www/res/icon/ios/icon-40.png" width="40" />

                                <icon height="80" platform="ios" src="www/res/icon/ios/icon-40@2x.png" width="80" />

                                <icon height="50" platform="ios" src="www/res/icon/ios/icon-50.png" width="50" />

                                <icon height="100" platform="ios" src="www/res/icon/ios/icon-50@2x.png" width="100" />

                                <icon height="60" platform="ios" src="www/res/icon/ios/icon-60.png" width="60" />

                                <icon height="120" platform="ios" src="www/res/icon/ios/icon-60@2x.png" width="120" />

                                <icon height="180" platform="ios" src="www/res/icon/ios/icon-60@3x.png" width="180" />

                                <icon height="72" platform="ios" src="www/res/icon/ios/icon-72.png" width="72" />

                                <icon height="144" platform="ios" src="www/res/icon/ios/icon-72@2x.png" width="144" />

                                <icon height="76" platform="ios" src="www/res/icon/ios/icon-76.png" width="76" />

                                <icon height="152" platform="ios" src="www/res/icon/ios/icon-76@2x.png" width="152" />

                                <icon height="29" platform="ios" src="www/res/icon/ios/icon-small.png" width="29" />

                                <icon height="58" platform="ios" src="www/res/icon/ios/icon-small@2x.png" width="58" />

                                <icon height="87" platform="ios" src="www/res/icon/ios/icon-small@3x.png" width="87" />

                                <splash height="1136" platform="ios" src="www/res/screen/ios/Default-568h@2x~iphone.png" width="640" />

                                <splash height="1334" platform="ios" src="www/res/screen/ios/Default-667h.png" width="750" />

                                <splash height="2208" platform="ios" src="www/res/screen/ios/Default-736h.png" width="1242" />

                                <splash height="1242" platform="ios" src="www/res/screen/ios/Default-Landscape-736h.png" width="2208" />

                                <splash height="1536" platform="ios" src="www/res/screen/ios/Default-Landscape@2x~ipad.png" width="2048" />

                                <splash height="768" platform="ios" src="www/res/screen/ios/Default-Landscape~ipad.png" width="1024" />

                                <splash height="2048" platform="ios" src="www/res/screen/ios/Default-Portrait@2x~ipad.png" width="1536" />

                                <splash height="1024" platform="ios" src="www/res/screen/ios/Default-Portrait~ipad.png" width="768" />

                                <splash height="960" platform="ios" src="www/res/screen/ios/Default@2x~iphone.png" width="640" />

                                <splash height="480" platform="ios" src="www/res/screen/ios/Default~iphone.png" width="320" />

                            </platform>

                            <platform name="wp8">

                                <icon height="99" platform="wp8" src="www/res/icon/wp8/ApplicationIcon.png" width="99" />

                                <icon height="159" platform="wp8" src="www/res/icon/wp8/Background.png" width="159" />

                                <splash height="1280" platform="wp8" src="www/res/screen/wp8/screen-portrait.jpg" width="768" />

                            </platform>

                            <platform name="windows">

                                <icon height="150" platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-100.png" width="150" />

                                <icon height="30" platform="windows" src="www/res/icon/windows/Square30x30Logo.scale-100.png" width="30" />

                                <icon height="50" platform="windows" src="www/res/icon/windows/StoreLogo.scale-100.png" width="50" />

                                <splash height="300" platform="windows" src="www/res/screen/windows/SplashScreen.scale-100.png" width="620" />

                                <icon height="120" platform="windows" src="www/res/icon/windows/StoreLogo.scale-240.png" width="120" />

                                <icon height="44" platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-100.png" width="44" />

                                <icon height="106" platform="windows" src="www/res/icon/windows/Square44x44Logo.scale-240.png" width="106" />

                                <icon height="70" platform="windows" src="www/res/icon/windows/Square70x70Logo.scale-100.png" width="70" />

                                <icon height="71" platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-100.png" width="71" />

                                <icon height="170" platform="windows" src="www/res/icon/windows/Square71x71Logo.scale-240.png" width="170" />

                                <icon height="360" platform="windows" src="www/res/icon/windows/Square150x150Logo.scale-240.png" width="360" />

                                <icon height="310" platform="windows" src="www/res/icon/windows/Square310x310Logo.scale-100.png" width="310" />

                                <icon height="150" platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-100.png" width="310" />

                                <icon height="360" platform="windows" src="www/res/icon/windows/Wide310x150Logo.scale-240.png" width="744" />

                                <splash height="1920" platform="windows" src="www/res/screen/windows/SplashScreenPhone.scale-240.png" width="1152" />

                            </platform>

                            <access origin="*" />

                            <allow-intent href="http://*/*" />

                            <allow-intent href="https://*/*" />

                            <allow-intent href="tel:*" />

                            <allow-intent href="sms:*" />

                            <allow-intent href="mailto:*" />

                            <allow-intent href="geo:*" />

                            <platform name="android">

                                <allow-intent href="market:*" />

                            </platform>

                            <platform name="ios">

                                <allow-intent href="itms:*" />

                                <allow-intent href="itms-apps:*" />

                            </platform>

                        </widget>

                         

                         

                        sorry for posting alot of code , its seems there is no strong support for phonegap online

                        • 9. Re: how to i create , write and save a file with cordova and phonegap ?
                          VectorP Level 4

                          Well, there is some support in this forum, but since you're building locally and testing in a browser, there's not much anyone can do for you.

                          You might consider using Phonegap Build, then show the zip file you are uploading. That way, forum participants can offer relevant help.

                           

                          If you want to get to the device file system, then you need a device, not a browser on a windows desktop.

                           

                          From your config, I can't tell how you added all those plugins. Did you merely enter the cordova-plugin-file in config, without installing that plugin locally? And if so, why did you pick an older version of that plugin and not the latest (which is currently 4.3.1)?

                          Also, if you have the file-transfer plgin, you don't need the file plugin, as it's already integrated.

                           

                          From the posted code, I can't verify whether you are waiting for the deviceready event or not.

                           

                          Do you really need all those other plugins? If not, remove the ones you don't need.

                           

                          Unrelated:

                          - you are missing yuite a bunch of iOS splash dimensions. Both portrait and (even more) landscape

                          - your android splashes won't show, since you are specifying compound qualifiers in a density attribute.

                          • 10. Re: how to i create , write and save a file with cordova and phonegap ?
                            ahmadalattar Level 1

                            hello ,

                             

                            actually phonegap did the config build for me , and some lines i did my self , because am trying to fix my problem.

                            i think your right , because i think the old version doesnt support getFile()

                             

                            ill try to do my best , but keep in mind am testing is with phonegap server app , on android from playstore. its a mobile simulator.

                            • 11. Re: how to i create , write and save a file with cordova and phonegap ?
                              ahmadalattar Level 1

                              its seems , i coudnt detect the problem yet, but now ive tried to build it throug build.adobe.com

                              and after i downloaded the apk file , it hangs in the initialisation phase

                               

                              any hint ?

                              • 12. Re: how to i create , write and save a file with cordova and phonegap ?
                                VectorP Level 4

                                Without seeing any code, I'm not even sure what that "initialisation phase" is.

                                Is your zip file available online somewhere, so I (and others) can have a look?

                                • 13. Re: how to i create , write and save a file with cordova and phonegap ?
                                  ahmadalattar Level 1

                                  hello ,

                                   

                                  thank you for the reply , ive delete the application and tried to re-upload it , and asked me for payment, and the payment dont accept my country..

                                   

                                  i think ill switch for ionic 2 , phonegap was great experience.

                                   

                                  thanks

                                  • 14. Re: how to i create , write and save a file with cordova and phonegap ?
                                    ahmadalattar Level 1

                                    PROBLEM HAS BEEN [SOLVED] :

                                     

                                    1. Config.xml : had to add :

                                    <preference name="AndroidPersistentFileLocation" value="Internal" />

                                    <preference name="AndroidPersistentFileLocation" value="Compatibility" /> 

                                    <preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />

                                    <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />

                                     

                                    2. webrowser & phoneGap mobile Server : WONT WORK

                                         but similator from android studio will work even writing the file

                                     

                                    3. build on adobe phoneGap : must add config.xml into /www/ then compress the content LEGACY in build docs and upload it.