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.
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With