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.
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:
- In JavaScript:
- Scroll the file upload button into view,
Element.scrollIntoView()
- Calculate the co-ordinates of the file upload button,
Element.getBoundingClientRect()
- Return the co-ordinates to C#.
- In C#:
- Programmatically send a mouse click to the co-ordinates (see this question)