I have a CEF WinForm Application which loads a webpage for creating helpdesk tickets. I want the user to be able to click a SCREENSHOT button and have it save the file to the desktop and then automatically attach it to the webform inside the CHROMIUM Browser control.
I can easily save the file to the desktop, that is easy and not a problem. I have also tried
browser.DialogHandler = new TempFileDialogHandler(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Capture.jpg");
This will take the file (CAPTURE.JPG) and attach it to the browser control, but not to the actual web page.
Can I use the javascript (script) option and somehow programmatically put this file into the FileUpload control in the page (see below)
I know I can have the user just click the button and manually add the file themselves, but I want to automate this process. Any help or direction would be greatly appreciated.
**** Edited Jan 7, 2021.
I tried to use the following code to use the DOM objects.
using (CefSharp.DevTools.DevToolsClient client = browser.GetDevToolsClient())
{
Task<GetDocumentResponse> dom = client.DOM.GetDocumentAsync();
QuerySelectorResponse querySelectorResponse = await client.DOM.QuerySelectorAsync(dom.Result.Root.NodeId, "fileupload");
_ = client.DOM.SetFileInputFilesAsync(new string[] { @"c:\temp\scr.png" }, querySelectorResponse.NodeId);
}
I am using CefSharp WinForms v83. When I run the code, it hangs on the
await client.DOM.QuerySelectorAsync(dom.Result.Root.NodeId, "fileupload");
Do I need event handlers?
Edit Jan 7 2021 #2 The Following changes to the code made it work once.
await Task.Run(async () =>
{
using (CefSharp.DevTools.DevToolsClient client = browser.GetDevToolsClient())
{
var dom = client.DOM.GetDocumentAsync();
var querySelectorResponse = await client.DOM.QuerySelectorAsync(dom.Result.Root.NodeId, "#fileupload");
_ = client.DOM.SetFileInputFilesAsync(new string[] { @"C:\Users\Chris\Desktop\capture.jpg" }, querySelectorResponse.NodeId);
_ = client.DOM.SetFileInputFilesAsync(new string[] { @"C:\Users\Chris\Desktop\capture1.jpg" }, querySelectorResponse.NodeId);
}
});
When I try to do it again, I get the follow error: Generated MessageId 100005 doesn't match returned Message Id 100001
** Edit Jan 7 2021 #3 - I think this works.
if (client == null)
{
client = browser.GetDevToolsClient();
dom = client.DOM.GetDocumentAsync();
}
await Task.Run(async () =>
{
var querySelectorResponse = await client.DOM.QuerySelectorAsync(dom.Result.Root.NodeId, "#fileupload");
_ = client.DOM.SetFileInputFilesAsync(new string[] { filename }, querySelectorResponse.NodeId);
});