Hi!
I have benn trying to download a pdf with filetransfer plugin. But it seemed as if cordova wasn’t being added to the project, however I added follow the documentation. My controller code is:
(function () {
"use strict";
angular.module('starter').controller('presentacionController', presentacionController);
presentacionController.$inject = ['$q', '$scope', '$state', '$timeout', '$cordovaFileTransfer'];
function presentacionController($q, $scope, $state, $timeout, $cordovaFileTransfer) {
// <editor-fold desc="Vars">
// </editor-fold>
// <editor-fold desc="Init">
function init() {
$scope.descargarPdf();
}
// </editor-fold>
// <editor-fold desc="Methods">
$scope.descargarPdf = function () {
document.addEventListener('deviceready', function () {
var url = "http://www.farmaforum.es/API_REST/presentacion-2017.pdf";
var fileDir = cordova.file.documentsDirectory + "presentacion-2017.pdf";
var download = $cordovaFileTransfer.download(url, fileDir).then(function (success) {
console.log("success " + JSON.stringify(success));
$timeout(function () {
$scope.downloadProgress = 100
}, 1000);
}, function (error) {
console.log("Error " + JSON.stringify(error));
}, function (progress) {
$timeout(function () {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
});
});
if ($scope.downloadProgress > 0.1) {
//download.abort();
}
})
};
// </editor-fold>
// <editor-fold desc="Helpers">
// </editor-fold>
// <editor-fold desc="Events">
// </editor-fold>
init();
}
})();
I am compiling with ionic package. When I try the app in my mobile device, the error log is:
TypeError: Cannot read property 'documentsDirectory' of undefined
at null.<anonymous> (file:///android_asset/www/app/controllers/presentacionController.js?ionicCachebuster=90118:33:43)
at Channel.subscribe (file:///android_asset/www/cordova.js?ionicCachebuster=90118:760:11)
at document.addEventListener (file:///android_asset/www/cordova.js?ionicCachebuster=90118:133:34)
at HTMLDocument.document.addEventListener (file:///android_asset/www/cordova.js?ionicCachebuster=90118:1665:13)
at Scope.$scope.descargarPdf (file:///android_asset/www/app/controllers/presentacionController.js?ionicCachebuster=90118:31:22)
at init (file:///android_asset/www/app/controllers/presentacionController.js?ionicCachebuster=90118:23:20)
at new presentacionController (file:///android_asset/www/app/controllers/presentacionController.js?ionicCachebuster=90118:83:9)
at Object.instantiate (file:///android_asset/www/lib/ionic/js/ionic.bundle.js?ionicCachebuster=90118:18010:14)
at $controller (file:///android_asset/www/lib/ionic/js/ionic.bundle.js?ionicCachebuster=90118:23412:28)
at self.appendViewElement (file:///android_asset/www/lib/ionic/js/ionic.bundle.js?ionicCachebuster=90118:59900:24)
In other hand, when I remove this line var fileDir = cordova.file.documentsDirectory + "presentacion-2017.pdf";
and change it for a complete path. The error shows that FileTransfer is not defined.
Anyone can help me please? u.u
Thanks in advance.