Table of Contents
Customizing File Dialog

You can provide a custom file dialog for the following two scenarios by handling the WebView's FileDialog event:

  • When a new download needs starts and the browser needs to prompt for a save as file name;
  • When user clicks the "Browse" button of a file upload element;

The following code demonstrates how to handle the "Save As" dialog:

//Connect the event handler
page.WebView.FileDialog += new FileDialogHandler(WebView_FileDialog);

void WebView_FileDialog(object sender, FileDialogEventArgs e)
{
    if (e.Mode == FileDialogMode.Save)
    {
        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = e.DefaultFileName;

        Nullable<bool> result = dlg.ShowDialog(this);
        if (result.HasValue && result.Value)
            e.Continue(dlg.FileName);
        else
            e.Cancel();

        //Mark the event as handled so that the default implementation
        //will not be used
        e.Handled = true;
    }
}

To use your own custom dialog, you must set the FileDialogEventArgs's Handled to true. You also must either call the FileDialogEventArgs's Continue or Cancel to continue or cancel the event that triggered the file dialog. If you call Continue, you must also provide a file name. Both Continue and Cancel can be called after your event handler has returned.