Reputation: 1749
Related to: https://stackoverflow.com/questions/21044197/download-file-and-store-them-locally-in-sdcard-using-phonegapbuild but that questions has never been answered.
About this app, I'm writing a little app that displays a bunch of pdf's, it's written using jquerymobile + phonegap and it's being build with the phonegap build service.
I realize that this function cannot work, since the root-Directory is not a valid place to write anything. But, I was unable to get the working directory of my application, nor was I able to figure out what exactly Error Code 1 meant using the documentation.
from config.xml:
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="permissions" value="none" />
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/device" />
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
And the build service tells me that these plugins have been sucessfully added.
Unpacking the apk and looking into the manifest also reveals that
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
was successfully added.
last but not least the phonegap library was successfully added to the document using
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
and then there is only the actual function left that would be to blame:
function downloadFile(name, url, y,x, canvasid, wrapperid){
alert(name);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
var directoryEntry = fileSystem.root; // to get root path to directory
var rootdir = fileSystem.root;
var fp = rootdir.fullPath;
fp = fp+"/bestsongs/"+name;
alert("downloading: " + url + " to " + fp);
var fileTransfer = new FileTransfer();
alert("downloading: " + url + " to " + fp);
fileTransfer.download(
url,
name,
function(theFile) {
alert("download complete: " + theFile.toURI());
loadPDF(theFile.toURI(), y,x, canvasid, wrapperid);
},function(error) {
alert("error: " + JSON.stringify(error));
}
);
},
function(arg) {
alert("FAIL ON requestFileSystem" + JSON.stringify(arg)); }
);
}
$(function() {
document.addEventListener("deviceready", function() {
alert("deviceready!");
try {
alert(url);
downloadFile(lyrics,url,y,x, 'the-canvas', 'wrapper');
} catch(e) {
alert(e);
}
}, false);
});
Using the alerts, here is what I get:
deviceready!
<url> to file (which is right)
the name of the file.
downloading <url> to //bestsongs/<name>
downloading <url> to //bestsongs/<name>
error: {
"code":1,
"source":<url>,
"target":<name>,
"http_status": null,
"body": null
}
I have been debugging this for days, please someone have the solution.
Upvotes: 2
Views: 7678
Reputation: 2180
Hey take a look at my answer over here -
Phonegap - Save image from url into device photo gallery
and let me know. If there is still any problem. Include following in your config.xml
For Android -
<feature name="http://api.phonegap.com/1.0/file" />
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
<feature name="Storage">
<param value="org.apache.cordova.Storage" name="android-package"/>
</feature>
For IOS-
<plugin name="File" value="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />
For checking network connection available or not add following in your config.xml - add permissions in plugin.xml and plugins name in config.xml
Plugin to check Network connection For Android -
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
For IOS -
<plugin name="NetworkStatus" value="CDVConnection" />
Upvotes: 2