How to download when application runs on mobile


#1

Hi guys,

I have an app which run pretty well on Browser. But when I run it in mobile I’m facing an issue.

Basically I download a file by doing a document.location.href on a .php file which give me the file I want. This solution is good because it runs on every browser.

The problem is this solution doesn’t work on mobile (I tested it only on android).

So do you have an idea on how I could do it ?

Thanks a lot


#2

You need this plugin: cordova-plugin-file-transfer ( https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/ )
I’m going to look the details on the weekend because i need this too. Maybe i could share a gist later


#3

I have used cordova-plugin-fastrde-downloader. It works on both Android and iOS.


#4

Thank you guys. Can you put a demo code please as I have no idea how to conditionally call a cordova plugin ?


#5

There are two steps. If you look at the plugin documentation you will get information.

Initialise at startup when mobile.

downloader.init({ folder: "your folder" });

use it to set the application directory on mobile where download will happen.

change the code based on the where it is going to execute.

{{#if mobilechk}} <a class="dnbtn" data-down="{{src}}" role="button">&nbsp;<i class="fa fa-download"></i></a> {{else}} <a href="{{src}}" download target="_blank">&nbsp;<i class="fa fa-download"></i></a> {{/if}}I

for mobile trap the event and use the plugin after getting the attribute value data-down which has the URL to download

`‘click .dnbtn’: function(e,tmpl) {
var a = e.currentTarget;
var down = $(a).attr(‘data-down’);
downloader.get(down);
e.preventDefault();
}

`