4

We are using CefSharp (v37.0.0 NuGet package) in a C# WinForms application. We are looking for a way to (programmatically) trigger file upload actions. How, if at all, can that be done? A couple of additional thoughts:

  • We have a file upload dialog on a page where, normally, a user would select a file. This works fine, we intercept the dialog call via IDialogHandler and provide the file.
  • Now we want to trigger the same dialog and pre-set the file. While the dialog opens successfully from an "ExecuteScriptAsync" call, we are unable to pre-select the file. Possibly due to the following reason: how to create/initialize the file object using file path html5
  • Our next idea was to have the dialog open normally and issue a click event programmatically so the browser would open the file dialog (which we would then catch via IDialogHandler and provide the file we desire).
  • Trying this approach, our observation is as follows:
  • Following a user action (e.g. button click inside the browser), we can programmatically "click" the file input button.
  • Doing the same from a call originating via ExecuteScriptAsync, the dialog (or call in IDialogHandler) does not come up.
Community
  • 1
  • 1
lichtalberich
  • 215
  • 1
  • 4
  • 10

3 Answers3

4

The file TempFileDialogHandler on the CefSharp sample project has an example of this.

    public class TempFileDialogHandler : IDialogHandler
    {
        public bool OnFileDialog(IWebBrowser browserControl, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)
        {
            callback.Continue(selectedAcceptFilter, new List<string> { Path.GetRandomFileName() });

            return true;
        }
    }


Replace Path.GetRandomFileName() for your filepath and then attach the handler to the browser like this:

browser.DialogHandler = new TempFileDialogHandler();
Alberto Rojas
  • 41
  • 1
  • 3
3

Doing the same from a call originating via ExecuteScriptAsync, the dialog (or call in IDialogHandler) does not come up.

This is probably due to the same security restrictions that apply to normal JavaScript: Programmatically open upload file dialog in Chrome

I think you will need to:

  1. In JavaScript:
    1. Scroll the file upload button into view, Element.scrollIntoView()
    2. Calculate the co-ordinates of the file upload button, Element.getBoundingClientRect()
    3. Return the co-ordinates to C#.
  2. In C#:
    1. Programmatically send a mouse click to the co-ordinates (see this question)
Community
  • 1
  • 1
Yoshi
  • 3,325
  • 1
  • 19
  • 24
  • 1
    That's definitely an obvious way to do it - thanks for pointing that out Yoshi! We have solved it in a slightly different way now. We let the user click our final "Save" button on the html form. But in this special case we open the "file open" dialog - which works because it originated from a user action -, which we then catch from C# and set the file appropriately. Next, we call the save function again right from the same C# event handler, which will finally run the save/upload logic. Cool stuff this tight interaction between javascript/.NET. – lichtalberich Dec 29 '14 at 07:41
-2

I might be wrong but this also can be achieve by using jquery file uploader plugins rather than doing it yourself.

Ajax Upload Uploadify

Akshay
  • 1