question

SrisharanNarayanprabhu-7010 avatar image
0 Votes"
SrisharanNarayanprabhu-7010 asked NarayanaPrabhuSriSharan-4813 edited

WebView2 - Download Multiple Files ? POPUP Issue

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

         }

            

     }


[2]: /answers/storage/attachments/170619-image.png

[3]: /answers/storage/attachments/170619-image.png

ms-edge
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Firedog avatar image
0 Votes"
Firedog answered remeshpatel-0276 edited

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?


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Firedog You are a genius mate ! Just the direction I needed. There is infact a preferences file inside WebView2 user data folder (userdata\EBWebView\Default\Preferences) .

Modified "automatic_downloads":{}
To "automatic_downloads":{"https://www.bseindia.com,*":{"expiration":"0","last_modified":"13288471025939328","model":0,"setting":1}} within preferences file.

Now when I run the app , the popup no longer appears for the website and all files are downloaded !

0 Votes 0 ·
Firedog avatar image Firedog SrisharanNarayanprabhu-7010 ·

Happy to help!

0 Votes 0 ·
remeshpatel-0276 avatar image remeshpatel-0276 SrisharanNarayanprabhu-7010 ·

@SrisharanNarayanprabhu-7010 , 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?

0 Votes 0 ·
XuDongPeng-MSFT avatar image
0 Votes"
XuDongPeng-MSFT answered MukeshMishra-8402 commented

Hi @SrisharanNarayanprabhu-7010,

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.


image.png (40.6 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @XuDongPeng-MSFT


I have tried what you suggested ,but unable to get the edge settings to work for the WebView2 control , although both run with the same profile. I am not sure if this has got to do with the user data folder locations. The WebView2 user data folder resides in the app location (C:\src\DataCollection\trunk\WebMining\WebCrawler.WebView2ConsoleApp\WebCrawler.WebView2ConsoleApp\bin\Debug\userdata) where as the edges user data resides in C:\Users\snarayanaprabhu\AppData\Local\Microsoft\Edge\User Data.

Also tried passing null to CoreWebView2Environment.CreateAsync i.e., used CoreWebView2Environment.CreateAsync(null, null, null) instead of CoreWebView2Environment.CreateAsync(null, useDataFolder, null) in the above code, but no luck.

0 Votes 0 ·
XuDongPeng-MSFT avatar image XuDongPeng-MSFT SrisharanNarayanprabhu-7010 ·

Hi @SrisharanNarayanprabhu-7010 ,

Well, if that's the case, you can try adding the site to the Allow List in the settings page mentioned above and see if that works for you.

0 Votes 0 ·

Hi @XuDongPeng-MSFT ,

I have observed that no setting applied on the edge browser gets replicated to Webview2 control . Both webview2 and Edge run with the same user profile (my windows account). Is there a way I can get the edge browser settings to apply to webview2? If not , Any tested workaround or a fix would be really helpful.

0 Votes 0 ·
Show more comments
remeshpatel-0276 avatar image
0 Votes"
remeshpatel-0276 answered NarayanaPrabhuSriSharan-4813 edited

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?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I havent tried changing the file programmatically since its a one time change. But its wierd that programmatic change doesnt work . Ensure you are using admin rights for that account that changes the file programmatically.

0 Votes 0 ·