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:

C#

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();
            }
        }
    }
}

VB.NET

Imports System.Threading
Imports DotNetBrowser
Imports DotNetBrowser.Events

Module DownloadSample
    Sub Main()
        Using browser As Browser = BrowserFactory.Create()
            Dim downloadHandler = new SampleDownloadHandler()
            browser.DownloadHandler = downloadHandler

            browser.LoadURL("ftp://ftp.teamdev.com/updates/jxbrowser-4.0-beta.zip")
            downloadHandler.Wait()
        End Using
    End Sub

    Friend Class SampleDownloadHandler
        Implements DownloadHandler

        Private Dim waitEvent As ManualResetEvent = new ManualResetEvent(False)

        Public Function AllowDownload(download As DownloadItem) As Boolean Implements DownloadHandler.AllowDownload
            AddHandler download.DownloadEvent, sub(sender As Object, e As DownloadEventArgs)
                Dim downloadItem As DownloadItem = e.Item
                
                If downloadItem.Completed Then
                    Console.Out.WriteLine("Download is completed!")
                    waitEvent.Set()
                End If
            End sub

            Console.Out.WriteLine("Destination file: " +
                                  download.DestinationFile)
            Return True
        End Function

        Public Sub Wait()
            waitEvent.WaitOne()
        End Sub
    End Class
End Module


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.