Working with the Download Handler in DotNetBrowser
In this tutorial we are talking about working with the download handler in DotNetBrowser.
Get a free 30-days DotNetBrowser trial licence: https://goo.gl/xs4sTU
About DotNetBrowser:
DotNetBrowser allows embedding a Chromium-based browser to your .NET app to display and process HTML5, CSS3, JavaScript, Flash etc.
Previous tutorials: https://goo.gl/ikESMu
Transcript:
00:05 We need to create a WPF solution, reference the DotNetBrowser library using NuGet and add the license file. You can find detailed instructions in our previous tutorials.
00:17 MainWindow markup.
Add the DockPanel and set the LastChildFill property value to ‘true’.
00:25 Add the StackPanel with horizontal orientation to the DockPanel and set its ‘DockPanel.Dock’ property value to ‘Top’.
00:37 Inside the StackPanel, create a TextBox named ‘AddressBox’ and set its width.
00:48 Next to the TextBox, create a button, set its name and text and create its ‘Click’ event handler.
01:04 Finally, add the WPFBrowserView to the DockPanel, set its name and the initial URL.
01:21 Implementing the MainWindow behaviour.
Create the ‘SampleDownloadHandler’ class inherited from the ‘DownloadHandler’ interface and generate the method stubs.
01:40 Create the bool ‘AskForDownload’ method and define its string argument. This method will display the message box if the Browser attempts to download a file and return the user’s choice.
01:54 Inside this method display the message box with the file name which will be passed as the method’s argument using the current dispatcher.
02:01 Return true if the user answers ‘Yes’.
02:31 Create the void ‘ShowDownloadCompleteMessage’ with the same argument. This method will display the message if the download is completed and open the downloaded file in the explorer.
02:56 Using the current dispatcher, display the message box with the passed argument and then call the Process.Start method with the ‘explorer’ argument.
03:46 Create the void ‘ShowDownloadCancelMessage’ method. Inside this method show the warning message with the passed file url.
04:36 Go to the ‘AllowDownload’ method. This method should return true if you want to download the file from the URL specified in the ‘downloadItem.URL’ argument property.
04:46 Create the bool ‘download’ variable and fill it using the ‘AskForDownload’ method call. Pass the ‘download item.URL’ as its argument. If the user allows the download, create the download event handler for the specified download item.
05:20 In this handler, if the download has been completed, call the ‘ShowDownloadCompleteMessage’ method.
05:31 Pass the ‘args.Item.DestinationPath’ property value as its argument.
05:43 Otherwise, if the user declines the download, call the ‘ShowDownloadCancelMessage’ method.
05:56 Return the value of the ‘download’ variable.
06:01 Inside the ‘MainWindow’ constructor after the ‘InitializeComponent’ method call, initialize the ‘BrowserView.Browser.DownloadHandler’ property with the new instance of the ‘SampleDownloadHandler’ class.
06:13 Finally, go to the button click handler and call the ‘BrowserView.Browser.LoadURL’ method with the text box content.
06:32 Build and launch
Let’s try to download something. If you try to load the file which exists but cannot be displayed, DotNetBrowser attempts to download this file. After the download is completed, the created download handler opens the downloaded file in the explorer window.
#DotNetBrowser
#EmbeddedBrowsers