Skip to content

Instantly share code, notes, and snippets.

@Igor-lkm
Created January 16, 2015 14:21
Show Gist options
  • Save Igor-lkm/94adfb2daed65ed6f18c to your computer and use it in GitHub Desktop.
Save Igor-lkm/94adfb2daed65ed6f18c to your computer and use it in GitHub Desktop.
fileSrvc to download files for cordova-plugin-file-opener2
'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