Loading plugins for files in Cordoba always gives http status 401 and error code 3
I use Ionic to build an Android app and when testing from the device, the call I make with the plugin org.apache.cordova.file-transfer
always gives the same answer.
Here's the code used to start 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 answer (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 saw others post that downgrading the plugin made it work, but when I downgrade below the current version, use (0.5), the app doesn't build. When I use the newest version (1.0), the application is created, but after starting it, the console says:
Uncaught module cordova-plugin-file.ProgressEvent not found - cordova.js:59
The device is connected and verified with the 'device' plugin.
Please, help!
source to share
Eventually, I loaded Angular into a device ready event in my main index.html file. Once the event was fired, I didn't have to worry about the plugins not initializing and not ready to use, which I think is one of the reasons the FileTransfer plugin was failing.
Another thing I did was install and use the command $iconic
for everything . I used to mix $cordova [cmd]
and $ionic [cmd]
. Not sure if this really matters, but now everything works correctly.
Below is the bootstrap logic. I personally believe that every ionic project should be created from the beginning. Just remember to remove the ng app from your body tag or wherever you put 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']);
}
});
The latest versions of Cordova and Ion are still in use. :)
source to share