Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
Using DotNetBrowser DialogHandler
API you can handle the situation when web page needs to display File Open dialog (e.g. when the user clicks File Input element on a web form). Using this API you can display your own File Open dialog or suppress the dialog at all and provide the path to a file program.
Note: By default DotNetBrowser displays standard OpenFileChooser dialog.
The following sample demonstrates how to modify this logic:
C#
using DotNetBrowser; using DotNetBrowser.Events; using DotNetBrowser.WPF; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace FileUploadSample { class Program { public class WindowMain : Window { private WPFBrowserView browserView; public WindowMain() { Browser browser = BrowserFactory.Create(); browserView = new WPFBrowserView(browser); browser.DialogHandler = new SampleDialogHandler(this); Content = browserView; Width = 1024; Height = 768; this.Loaded += WindowMain_Loaded; } void WindowMain_Loaded(object sender, RoutedEventArgs e) { browserView.Browser.LoadHTML("http://jkorpela.fi/forms/file.html"); } public class SampleDialogHandler : WPFDefaultDialogHandler { private Window windowMain; public SampleDialogHandler(Window windowMain) : base(windowMain) { // TODO: Complete member initialization this.windowMain = windowMain; } public override CloseStatus OnFileChooser(FileChooserParams parameters) { CloseStatus returnValue = CloseStatus.CANCEL; bool isOpenMode = parameters.Mode == FileChooserMode.Open; bool isOpenMultipleMode = parameters.Mode == FileChooserMode.OpenMultiple; if (isOpenMode || isOpenMultipleMode) { Application.Current.Dispatcher.Invoke((Action)(() => { OpenFileDialog fileChooser = new OpenFileDialog(); fileChooser.Multiselect = isOpenMultipleMode; fileChooser.Title = "Open"; fileChooser.FileOk += delegate { if (isOpenMultipleMode) { string[] selectedFiles = fileChooser.FileNames; parameters.SelectedFiles = String.Join("|", selectedFiles); } else { parameters.SelectedFiles = fileChooser.FileName; } returnValue = CloseStatus.OK; }; fileChooser.ShowDialog(windowMain); })); } return returnValue; } } [STAThread] public static void Main() { Application app = new Application(); WindowMain wnd = new WindowMain(); app.Run(wnd); var browser = wnd.browserView.Browser; wnd.browserView.Dispose(); browser.Dispose(); } } } }
VB.NET
Imports System.Windows Imports DotNetBrowser Imports DotNetBrowser.WPF Imports Microsoft.Win32 Module Module1 Public Class WindowMain Inherits Window Public Dim browserView As WPFBrowserView Public Sub New() Dim browser As Browser = BrowserFactory.Create() browserView = New WPFBrowserView(browser) browser.DialogHandler = New SampleDialogHandler(Me) Content = browserView Width = 1024 Height = 768 AddHandler Me.Loaded, AddressOf WindowMain_Loaded End Sub Private Sub WindowMain_Loaded(sender As Object, e As RoutedEventArgs) browserView.Browser.LoadURL("http://jkorpela.fi/forms/file.html") End Sub Public Class SampleDialogHandler Inherits WPFDefaultDialogHandler Private windowMain As Window Public Sub New(windowMain As Window) MyBase.New(windowMain) Me.windowMain = windowMain End Sub Public Overloads Function OnFileChooser(ByVal parameters As FileChooserParams) As CloseStatus Dim returnValue As CloseStatus = CloseStatus.CANCEL Dim isOpenMode As Boolean = parameters.Mode = FileChooserMode.Open Dim isOpenMultipleMode As Boolean = parameters.Mode = FileChooserMode.OpenMultiple If isOpenMode Or isOpenMultipleMode Then System.Windows.Application.Current.Dispatcher.Invoke(Sub() Dim fileChooser As OpenFileDialog = New OpenFileDialog() fileChooser.Multiselect = isOpenMultipleMode fileChooser.Title = "Open" AddHandler fileChooser.FileOk, sub() If isOpenMultipleMode Then Dim selectedFiles As String() = fileChooser.FileNames parameters.SelectedFiles = String.Join("|", selectedFiles) Else parameters.SelectedFiles = fileChooser.FileName End If returnValue = CloseStatus.OK End sub fileChooser.ShowDialog(windowMain) End Sub) End If Return returnValue End Function End Class End Class <STAThread> Sub Main() Dim app As Application = New Application() Dim wnd As WindowMain = New WindowMain() app.Run(wnd) Dim browser = wnd.browserView.Browser wnd.browserView.Dispose() browser.Dispose() End Sub End Module