When trying to download multiple files over a website using webvew2 . We see the below popup message. I am unable to programmatically select the allow action on this popup. Is there a way i can programmatically override this popup message OR set a property in objWebView2.CoreWebView2 object to allow download of multiple files by default ? Any suggestion here to get over the problem would be of great help.
- PFA popup message
We are trying to download multiple files from the same page after navigating to the webpage using the WebView2 object. Below is the sample code. We are successfully able to download the first file by clicking ContentPlaceHolder1_GridView2_imgBtn1_0 file. But we see the above popup when we click the ContentPlaceHolder1_GridView2_imgBtn1_1 file (second file) and the objWebView2_DownloadStarting event does not trigger thereafter.
Microsoft.Web.WebView2.WinForms.WebView2 objWebView2;
CoreWebView2DownloadOperation objCoreWebView2DownloadOperation;
Application.EnableVisualStyles();
var env = CoreWebView2Environment.CreateAsync(null, useDataFolder, null).Result;
var envtask = objWebView2.EnsureCoreWebView2Async(env);
objWebView2.CoreWebView2.DownloadStarting += objWebView2_DownloadStarting;
var src = obj.Navigate("https://www.bseindia.com/markets/MarketInfo/NoticesCirculars.aspx?id=0&txtscripcd=&pagecont=&subject=");
var result = objWebView2.CoreWebView2.ExecuteScriptAsync("setTimeout(function() { var ctr = document.getElementById('ContentPlaceHolder1_GridView2_imgBtn1_0'); " +
"var evt = new MouseEvent('build'); " +
"evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"ctr.dispatchEvent(evt); " +
" } , 500); ");
var result1 = objWebView2.CoreWebView2.ExecuteScriptAsync("setTimeout(function() { var ctr = document.getElementById('ContentPlaceHolder1_GridView2_imgBtn1_1'); " +
"var evt = new MouseEvent('build'); " +
"evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"ctr.dispatchEvent(evt); " +
" } , 500); ");
private void objWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
e.ResultFilePath = _downloadpath; // setting a local download path
e.Handled = true;
e.DownloadOperation.StateChanged += objWebView2_StateChanged;
objCoreWebView2DownloadOperation = e.DownloadOperation;
}
private void objWebView2_StateChanged(object sender, object e)
{
if (objCoreWebView2DownloadOperation.State == CoreWebView2DownloadState.Completed)
{
_isFileDownloaded = true; //setting some flag to notify download is complete
}
}
Happy to help!
@Sri sharan Narayan prabhu , I tried replacing automatic_downloads":{} as suggested and it works only when file is changed manually. However, when we do this programmatically (update file programmatically), this preference seems to be reverted after few seconds. Are you facing similar issue?