Migration

This guide describes how to manage file downloads, track a download progress, get a notification when the download has been completed, etc.

Overview

To manage file downloads use the DownloadService class:

DownloadServicedownloadService=engine.getDownloadService();

Every time the JxBrowser needs to download a file the DownloadCallback callback is invoked. In this callback you can decide whether the file should be downloaded or the download request must be canceled.

The following example demonstrates how to cancel all download requests:

Accepting Download

To change the default behavior and allow to download a file please use the following approach:

downloadService.set(DownloadCallback.class,(params,callback)->{// Download target details.DownloadTargetdownloadTarget=params.getDownloadTarget();// The absolute path to the destination file.StringdestinationFile=downloadTarget.getDestinationFile();// Tell the engine to download and save the file.callback.download(newFile(destinationFile));});

In the example above we tell the library that the file can be downloaded and saved to a given destination file.

Make sure that the Chromium main process has rights to create files in the given destination directory. If the given directory already has a file with the given name, then the file will be overridden automatically. We recommend that you check the rights and the file existence before accepting the file download.

Controlling Process

To control the download process use the Download class. In DownloadCallback you receive a unique identifier of the download request – DownloadId. Use this ID to access the Download object.

Pausing Download

Resuming Download

To resume the paused download use the Download.resume() method:

if(download.isPaused()){download.resume();}

Canceling Download

You can cancel the download anytime unless the download is in a terminal state. This includes the completed downloads, canceled downloads, and interrupted downloads that cannot be resumed. To find out whether download is in the terminal state please use the Download.isDone() method.

To cancel the download use the Download.cancel() method:

if(!download.isDone()){download.cancel();}

Download Events

You can track the download progress, get notifications when the download has been canceled, paused, interrupted, or finished.

Download Canceled

To get notifications when the Download has been canceled use the DownloadCanceled event:

download.on(DownloadCanceled.class,event->{});

Download Finished

To get notifications when the Download has been finished use the DownloadFinished event:

download.on(DownloadFinished.class,event->{});

Download Paused

To get notifications when the Download has been paused use the DownloadPaused event:

download.on(DownloadPaused.class,event->{});

Download Updated

To track the download progress use the DownloadUpdated event:

download.on(DownloadUpdated.class,event->{// Print download progress in percents.Progressprogress=event.getProgress();System.out.println(progress.getValue()+"%");// The current download speed estimate in bytes/second.longcurrentSpeed=event.getCurrentSpeed();// The total size of a file in bytes.longtotalBytes=event.getTotalBytes();// The number or received (downloaded) bytes.longreceivedBytes=event.getReceivedBytes();});

Download Interrupted

To get notifications when the Download has been interrupted for some reason use the DownloadInterrupted event: