3

Why it is so awfully difficult to download a file from a remote server into the Android app's folder using Phonegap?

It's almost a week that I'm trying. I read every single post and tried every example I found. I read about differences and bugs reported between different versions of Phonegap. I read and studied the official documentation about the cordova-plugin-file-transfer and the cordova-plugin-file. I read about configuring properly permissions and whitelisting in the config.xml file.

I also wondered that I didn'd see the downloaded file using the phone's File explorer because Android permissions set that folder as private so I rooted my phone.

But none of the above ever worked.

This is my code. Please, If you wish to give your kind contribute, do not simply copy/paste code from other sources - as I saw often - because I already did it by my self.

CONFIG.XML (I'm using Phonegap Build)

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns   = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id          = "com.my_app"
    versionCode = "10" 
    version     = "1.0.0" >

<name>MY_APP</name>

<description>
   File download test
</description>

<author href="http://enrico.io">
    Me
</author>

<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="iosPersistentFileLocation" value="Library" />
<preference name="BackupWebStorage" value="none" />

<plugin spec="https://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git" />
<plugin spec="https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git" />
<plugin name="cordova-plugin-whitelist" spec="1.2.2" />

<access origin="*" subdomains="true" />

</widget>

INDEX.HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: ms-appdata: http://www.enrico.io 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

        <title>Downloading file</title>

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

</head>
<body>
    MY_APP
    <div id="fs"></div>
    <div id="app_folder"></div>
    <div id="saving"></div>
    <div id="error_code"></div>
    <div id="error_source"></div>
    <div id="error_target"></div>


    <script type="text/javascript">

        document.addEventListener("deviceready", onDeviceReady, false);  

        function onDeviceReady() {  

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);

                function onFileSystemSuccess(fileSystem) {
                    //document.getElementById("fs").innerHTML = fileSystem.name;

                    var fileTransfer = new FileTransfer();
                    var fileURL = cordova.file.dataDirectory + "test.txt";

                    var uri = encodeURI("http://www.enrico.io/phonegap/test.txt");

                    document.getElementById("app_folder").innerHTML = "App folder = " + fileURL;

                    fileTransfer.download(
                        uri,
                        fileURL,
                        function(entry) {
                            document.getElementById("saving").innerHTML = "Saving to... " + entry.toURL();
                        },
                        function(error) {
                            document.getElementById("error_code").innerHTML = "Error code = " + error.code;
                            document.getElementById("error_source").innerHTML = "Error source = " + error.source;
                            document.getElementById("error_target").innerHTML = "Error target = " + error.target;
                        },
                        false,
                        {
                            headers: {
                                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                            }
                        }
                    );

                }

                function fail(evt) {
                    document.getElementById("fs").innerHTML = evt.target.error.code;
                }

        }  

</script>

OUTPUT IS:

MY_APP
App folder = file:///data/data/com.my_app/files/test.txt
Saving... = file:///data/data/com.my_app/files/test.txt

The file seems to have been correctly download but it has not. If I remove the file name (test.txt) from:

var fileURL = cordova.file.dataDirectory + "test.txt";

the OUTPUT IS:

MY_APP
App folder = file:///data/data/com.my_app/files/
Error code = 1
Error source = http://www.enrico.io/phonegap/test.txt
Error target = file:///data/data/com.my_app/files/

EDIT. As suggested by @Gandhi (see comment below) changing cordova.file.dataDirectory to cordova.file.externalRootDirectory it works but the file is downloaded to file:///storage/sdcard0/ and to not to the app's data directory/folder. Keep in mind the drawback: if the app is uninstalled the app's data is not deleted.

Nicero
  • 4,181
  • 6
  • 30
  • 52
  • I did understand that you have mentioned not to copy past code from links but i still suggest you have a look at these links as i m sure that it will help you out. http://stackoverflow.com/questions/35526078/cordova-file-plugin-has-wrong-directory http://stackoverflow.com/questions/37206408/opening-a-pdf-in-cordova-javascript/ – Gandhi May 17 '16 at 19:31
  • @Gandhi. Changing `cordova.file.dataDirectory` with `cordova.file.externalRootDirectory`worked and the file has been downloaded. Thank you. But I still would like to use the app's data folder so that if the app is uninstalled all the data downloaded is uninstalled too. I did not manage to make your second example work. – Nicero May 19 '16 at 13:38
  • could you be more specific on the issue with second example? Why dont you just wipe out the complete folder during application logout every time? – Gandhi May 19 '16 at 14:00
  • I placed your script inside the `onDeviceReady()` function, changed the path to the file to download and changed the call to `openFile()` with a simple `Alert` to know if the script has downloaded something but it did not display any `alert`. I have to double check. I do not want to wipe the app's folder on logout because I'm trying to do an app that downloads a json file with informations about events in my town that it should be read on start. In this way the app may work offline (rather than using a db). Every time it's ran the app checks the server for a new json file. – Nicero May 19 '16 at 14:19
  • could you inspect in chrome for any error in console? – Gandhi May 19 '16 at 14:28
  • I will check better but your "PDF" example still uses the `root` folder. Do you know how to use the app's data folder? I tried `cordova.file.dataDirectory` and `cordova.file.externalDataDirectory`(both map to file://data/data/app_id/files/) but none worked. If not possible any idea how to delete the app's files when uninstalled? – Nicero May 19 '16 at 14:45
  • have tried storing in app data directory earlier but no success. The only option was to root the device which I dint tried. But I do have code to wipe the folder. Will check more on how to invoke the same during uninstall and get back to you – Gandhi May 19 '16 at 15:33
  • I suggest you to have a look at this link - http://stackoverflow.com/questions/22294547/phonegap-file-transfer-error-1-where-to-write-filetransfers Along with "AndroidPersistentFileLocation" setting to "internal", few more settings is required in config,xml it seems. Have not tested personally though – Gandhi May 20 '16 at 12:25
  • After some googling, got to know that currently there is no provision in cordova app to identify app uninstall action. Let me know if i could assist you in resolving datadirectory write issue. – Gandhi May 20 '16 at 12:48

0 Answers0