Last active
May 2, 2016 18:18
-
-
Save jdnichollsc/1e26e9a1dea37889abbc693eda9bb1ee to your computer and use it in GitHub Desktop.
Download and Open File with ngCordova in Ionic Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular | |
.module('App') | |
.factory('Files', Files); | |
Files.$inject = ['$window', '$cordovaFileTransfer', '$ionicLoading', '$cordovaFileOpener2']; | |
function Files($window, $cordovaFileTransfer, $ionicLoading, $cordovaFileOpener2) { | |
return { | |
downloadCSVFile : function(urlFile, fileName, mimeType){ | |
if(ionic.Platform.isWebView()){ | |
$ionicLoading.show({ | |
template: '<ion-spinner class="spinner-energized" icon="lines"></ion-spinner>', | |
hideOnStageChange: true | |
}); | |
var targetPath = (cordova.file.documentsDirectory || cordova.file.externalRootDirectory) + fileName; | |
$cordovaFileTransfer.download(urlFile, targetPath, {}, true).then(function(result) { | |
return $cordovaFileOpener2.open(targetPath, mimeType); | |
}).then(function(){ | |
}).catch(function(err) { | |
alert(err); | |
}).finally(function(){ | |
$ionicLoading.hide(); | |
}); | |
}else{ | |
$window.location.href = urlFile; | |
} | |
} | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment