I am using WebView2 in a C# windows application and I want to be able to upload screen captures. I capture the screen and save it to a file (jpg) on the local hard drive. I want to attach this file (upload) it to the FILEUPLOAD control on a web page being displayed on a windows form using WebView2.
I was using CEFSHARP browser and was using DOM to upload the file before we switched to WebView2.
CEFSHAPRP had the DOM objects wrapped in the browser control so it was very easy to use this code :
if (client == null)
{
client = browser.GetDevToolsClient();
dom = client.DOM.GetDocumentAsync();
}
await Task.Run(async () =>
{
QuerySelectorResponse querySelectorResponse = await client.DOM.QuerySelectorAsync(dom.Result.Root.NodeId, "#fileupload");
_ = client.DOM.SetFileInputFilesAsync(new string[] { filename }, querySelectorResponse.NodeId);
});
I do not see anything like this built into the WebView2 control. I did see that WebView has another nuget called WebView2.DOM but it requires me to convert my entire project from .NET framework 4.7.2 to .netCore.
Any help would be greatly appreciated. I am very new to WebView2 and cannot see how I can access the DOM to make this happen. I am using .net framework 4.7.2
******* June 17 Update *****
I installed the Microsoft.Web.WebView2.DevToolsProtocolExtension and tried to use the existing framework I already had working with CEFSHARP.
DevToolsProtocolHelper helper = webView21.CoreWebView2.GetDevToolsProtocolHelper();
Task<DOM.Node> t = helper.DOM.GetDocumentAsync(-1,false);
await Task.Run(async () =>
{
var querySelectorResponse = await helper.DOM.QuerySelectorAsync(t.Result.NodeId, "#fileupload");
_ = helper.DOM.SetFileInputFilesAsync(new string[] { filename }, querySelectorResponse);
});
It gives me the following error when I try to execute the line :
var querySelectorResponse = await helper.DOM.QuerySelectorAsync(t.Result.NodeId, "#fileupload");
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Web.WebView2.Core.Raw.ICoreWebView2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{76ECEACB-0462-4D94-AC83-423A6793775E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Any ideas. Thank you for all the advice and help I have received already.