WebView2 - Download Multiple Files ? POPUP Issue

Sri sharan Narayan prabhu 21 Reputation points
2022-02-02T22:03:46.357+00:00

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.

170713-downloadmultiplefilepopup.png - 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  

        }  

          

    }  
Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,107 questions
0 comments No comments
{count} votes

Accepted answer
  1. Noel Burgess 626 Reputation points
    2022-02-04T12:15:52.88+00:00

    I can't help specifically with WebView2, but I'd point out that many Edge preferences (like Ask when a site tries to download multiple files automatically) are stored in the user's Preferences file, e.g. %localappdata%\Microsoft\Edge\User Data\Default\Preferences. It's a plain-text JSON file, so it should be easily manipulated programmatically. If there is a Preferences file in the WV2 userdata folder you found, could you edit it?


2 additional answers

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,096 Reputation points Microsoft Vendor
    2022-02-03T03:28:19.343+00:00

    Hi @Sri sharan Narayan prabhu ,

    Based on your description, I searched the relevant documentation, but found no such property (maybe I missed it). And as far as I know, Webview2 is based on Chromium Edge, I think the settings in Edge should affect WebView2 Control (this issue).

    For example this setting in Edge:

    170777-image.png

    This setting is enabled by default, which I guess should be the cause of the problem. You can try disabling it and check if this works for you.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. remeshpatel 1 Reputation point
    2022-02-16T02:51:39.437+00:00

    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. Is anyone facing similar issue?