lichtalberich lichtalberich - 27 days ago 43
C# Question

File uploads with CefSharp

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.


Answer

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)
Comments