quinta-feira, 24 de março de 2016

ionic - download file


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:

That's it :)
Adriano Schmidt

Nenhum comentário:

Postar um comentário