I will show you how to download a file from an URL to the device with Ionic.
Be attention: It will only work in a real device, don't use ionic serve, ionic view, ionic live-reload, -l, phonegap build services or anything like this.
Let's go! It is very simple:
First of all, you need to install 2 plugins:
cordova plugin add org.apache.cordova.file-transfer cordova plugin add org.apache.cordova.file
Now, you need to receive the parameter $cordovaFileTransfer:
.controller('MyCtrl', function($scope, $window, $cordovaFileTransfer) {
And here is the magic code:
function downloadFile() { var url = "http://www.localhost8080.com.br/site/img/portfolio-2.jpg"; var folder = 'localhost8080/'; var filename = url.split("/").pop(); var targetPath = window.cordova.file.externalRootDirectory + folder + filename; $cordovaFileTransfer.download(url, targetPath, {}, true).then( function (result) { var myNativeURL = result.toURL()); }, function (error) { }, function (progress) { } ); }
See more at:
http://www.gajotres.net/using-cordova-file-transfer-plugin-with-ionic-framework/
http://ngcordova.com/docs/plugins/fileTransfer/
https://www.thepolyglotdeveloper.com/2014/09/manage-files-in-android-and-ios-using-ionicframework/
That's it :)
Adriano Schmidt