Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger android download manager Cordova [closed]

I'm trying to download files with a Cordova app, this is working fine for smaller files using the File transfer plugin, but for bigger files I would like to trigger the android download manager. Is this possible(probably with a plugin)?

I need to be able to change the headers of the request, so the 'cordova plugin background download' plugin didn't help me.

like image 282
Nathan Avatar asked Sep 04 '16 09:09

Nathan


2 Answers

The best way to go is to write your own plugin. Writing Cordova plugins is easy, and it is enough just to follow official doc. We done the same because also it was necessary to customize download process. It looks like a native call using this method, because it activate native DM so user can see progress, cancel it, pause etc.

And once you register your own plugin this is code that you can use to start with:

    import android.app.DownloadManager;
        import android.app.DownloadManager.Request;

        public class DownloadPlugin extends CordovaPlugin{

        private DownloadManager downloadManager;

        public DownloadPlugin() {
           downloadManager = (DownloadManager) webView.getContext()
                                                               .getSystemService(webView.getContext().DOWNLOAD_SERVICE);
        }

        @Override
        public boolean execute(String action, JSONArray args,
                               CallbackContext callbackContext) throws JSONException {

          String downloadUrl = args.getString(0);
          startDownloadForUrl(String downloadUrl);

        }

        public void startDownloadForUrl(String downloadUrl){
                Request downloadRequest = new Request(downloadUrl);
                if (accessToken != null) { // we use oauth so this is example of changing download request
                    downloadRequest.addRequestHeader("Authorization", "Bearer "
                            + accessToken);
                }
                downloadRequest
                .setDescription("description")
                .setDestinationInExternalFilesDir("folderName", "fileName");
                downloadManager.enqueue(downloadRequest);  
        }

       }
like image 129
mommcilo Avatar answered Sep 21 '22 16:09

mommcilo


This is one readymade downloader plugin which matches your requirement and comparitively active.

It also provides various options for downloading as mentioned in the usage link

like image 31
Gandhi Avatar answered Sep 21 '22 16:09

Gandhi