DotNetBrowser provides API that allows handling all the file downloads. Using this API you can control what files should be downloaded, provide the path to the destination directory where the file must be saved and track download progress.


You can register your own implementation of theDownloadHandler interface to handle file downloads in your own way. The following sample demonstrates how to do this:


using DotNetBrowser;
using DotNetBrowser.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DownloadSample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Browser browser = BrowserFactory.Create())
            {

                var downloadHandler = new SampleDownloadHandler();
                browser.DownloadHandler = downloadHandler;

                browser.LoadURL("ftp://ftp.teamdev.com/updates/jxbrowser-4.0-beta.zip");
                downloadHandler.Wait();
            }
        }

        class SampleDownloadHandler : DownloadHandler
        {
            private ManualResetEvent waitEvent = new ManualResetEvent(false);

            public bool AllowDownload(DownloadItem download)
            {
                download.DownloadEvent += delegate(object sender, DownloadEventArgs e)
                {
                    DownloadItem downloadItem = e.Item;
                    if (downloadItem.Completed)
                    {
                        Console.Out.WriteLine("Download is completed!");
                        waitEvent.Set();
                    }
                };
                Console.Out.WriteLine("Destination file: " +
                        download.DestinationFile);                
                return true;
            }

            public void Wait()
            {
                waitEvent.WaitOne();
            }
        }
    }
}


The DownloadItemclass provides methods for pausing and canceling download process. See its corresponding methods.


Starting from DotNetBrowser 1.8.4 it is also possible to obtain the download interrupt reason that can help to detect a number of errors during download. See theDownloadItem.DownloadInterruptReason property and theDownloadInterruptReason enumeration.