Skip to content

Instantly share code, notes, and snippets.

@jdnichollsc
Last active May 2, 2016 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdnichollsc/1e26e9a1dea37889abbc693eda9bb1ee to your computer and use it in GitHub Desktop.
Save jdnichollsc/1e26e9a1dea37889abbc693eda9bb1ee to your computer and use it in GitHub Desktop.
Download and Open File with ngCordova in Ionic Framework
(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