Created
January 16, 2015 14:21
-
-
Save Igor-lkm/94adfb2daed65ed6f18c to your computer and use it in GitHub Desktop.
fileSrvc to download files for cordova-plugin-file-opener2
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
'use strict'; | |
angular.module('app') | |
.service('fileSrvc', fileSrvc); | |
fileSrvc.$inject = []; | |
function fileSrvc() { | |
return { | |
download: download | |
}; | |
//////////// | |
var download = function(url, callback) { | |
/* Get file extantion from URL*/ | |
var ext = url.split('.').pop(); | |
/* Generate file name */ | |
var fileInternal = new Date().getTime() + '.' + ext; | |
/* Detect device */ | |
var iosDevice = navigator.userAgent.match(/(iPhone|iPod|iPad)/i); | |
CordovaSrvc.ready.then(function() { | |
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { | |
var fileTransfer = new FileTransfer(); | |
var downloadPath; | |
/* Download path for iOS and android are different */ | |
if (iosDevice !== null) { | |
downloadPath = fileSystem.root.toURL() + fileInternal; | |
} else { | |
downloadPath = cordova.file.externalDataDirectory + fileInternal; | |
} | |
fileTransfer.download( | |
url, | |
downloadPath, | |
function(file) { | |
callback(null, file.nativeURL); | |
}, | |
function(error) { | |
callback(error); | |
}, | |
true | |
); | |
}, function(error) { | |
callback(error); | |
}); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment