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.