I'm using Ionic to build an app for Android, and when testing from a device, the call I make using the org.apache.cordova.file-transfer
plugin always yields the same response.
Here's the code used to fire off the download:
$scope.getFile = function(){
var filePath = cordova.file.dataDirectory + "beer.json";
var uri = encodeURI('https://stormy-sierra-8448.herokuapp.com/api/?q=stones+pale+ale');
var options = {};
$cordovaFileTransfer.download(uri, filePath, options, true)
.then(function(result) {
$scope.status = result;
alert('success');
}, function(err) {
console.log(err);
}, function (progress) {
$timeout(function () {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
})
});
}
And then the response (from the console)
FileTransferError {
code: 3,
source: "https://stormy-sierra-8448.herokuapp.com/api/?q=stones+pale+ale",
target: "documents/beer.txt",
http_status: 401,
body: null…
}
body: nullcode: 3exception: nullhttp_status: 401source: "https://stormy-sierra-8448.herokuapp.com/api/?q=stones+pale+ale"
target: "documents/beer.txt"
__proto__: FileTransferError
My environment looks like this: Cordova v5.0.0 Ionic 1.3.20
I've seen others post that downgrading the plugin made it work, but when I go below the current version I'm using (0.5), the app doesn't build. When I use the newest version (1.0), the app builds, but after it launches, the console says:
Uncaught module cordova-plugin-file.ProgressEvent not found - cordova.js:59
The device has a connection and verified with the 'device' plugin.
Please help!
In the end, I bootstrapped Angular to the device ready event in my main index.html file. Once the event fired, I didn't have to worry about plugins not being initialized and ready for use, which I think is one of the reasons the FileTransfer plugin was failing me.
Another thing I did was install and use the $iconic
command for everything. Before, I was mixing $cordova [cmd]
and $ionic [cmd]
. Not sure that it really matters, but everything seems to be working properly now.
The bootstrapping logic is below. I personally think this is how every ionic project should be set up from the get-go. Just remember to remove the ng-app from your body tag or wherever you've placed it.
angular.element(document).ready(function() {
if (window.cordova) {
document.addEventListener('deviceready', function() {
angular.bootstrap(document.body, ['myApp']);
}, false);
} else {
angular.bootstrap(document.body, ['myApp']);
}
});
Still using the latest versions of Cordova and Ionic. :)