Skip to content

Instantly share code, notes, and snippets.

@viktor-shmigol
Created February 26, 2016 13:11
Show Gist options
  • Save viktor-shmigol/926a01f608437304bc8c to your computer and use it in GitHub Desktop.
Save viktor-shmigol/926a01f608437304bc8c to your computer and use it in GitHub Desktop.
$scope.download = (url) ->
filename = url.split('/').pop()
url = encodeURI(url)
if ionic.Platform.isIOS()
directory = cordova.file.documentsDirectory
else
directory = "#{cordova.file.externalRootDirectory}Download/"
document.addEventListener 'deviceready', (->
$ionicLoading.show template: 'Loading...'
targetPath = directory + filename
trustHosts = true
options = {}
$cordovaFileTransfer.download(url, targetPath, options, trustHosts).then ((result) ->
$ionicLoading.hide()
toastr.success "You have successfully downloaded file to #{targetPath}!"
), ((err) ->
$ionicLoading.hide()
toastr.error 'Something went wrong! Try again'
), (progress) ->
), false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment