How to download a file on a UWP WebView2?

Viewed 370

I'm trying to support file downloads on a UWP WebView2.
Not sure whether I'm doing something wrong or if this a bug, but I keep getting state change update to Interrupted with reason UserCanceled.

Here's how I'm testing it on the main page:

public MainPage()
{
    this.InitializeComponent();
    wv2.EnsureCoreWebView2Async().AsTask().ContinueWith(async (task) =>
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
        () =>
        {
            wv2.CoreWebView2.DownloadStarting += OnDownloadStarting;
            wv2.CoreWebView2.Navigate("http://demo.borland.com/testsite/downloads/downloadfile.php?file=dotNetFx40_Full_x86_x64.exe&cd=attachment+filename");
        });
    });
}

private void OnDownloadStarting(Microsoft.Web.WebView2.Core.CoreWebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2DownloadStartingEventArgs args)
{
    Trace.WriteLine("DownloadStarting");
    var downloadOp = args.DownloadOperation;
    args.DownloadOperation.StateChanged += (sender2, args2) =>
    {
        var state = downloadOp.State;
        switch (state)
        {
            case Microsoft.Web.WebView2.Core.CoreWebView2DownloadState.InProgress:
                Trace.WriteLine("Download StateChanged: InProgress");
                break;
            case Microsoft.Web.WebView2.Core.CoreWebView2DownloadState.Completed:
                Trace.WriteLine("Download StateChanged: Completed");
                break;
            case Microsoft.Web.WebView2.Core.CoreWebView2DownloadState.Interrupted:
                Trace.WriteLine("Download StateChanged: Interrupted, reason: " + downloadOp.InterruptReason);
                break;
            }
        };
    }

I also tried setting the ResultFilePath to the TemporaryFolder or to the user's DownloadsFolder and gave the app these restricted capabilities <rescap:Capability Name="broadFileSystemAccess" /> and <rescap:Capability Name="runFullTrust" />.
But I consistently keep getting a single state update to the same Interrupted UserCanceled error.

Here's a sample project demonstrating the problem: https://github.com/nirbil/WebView2FileDownload

Any ideas?

2 Answers

But I consistently keep getting a single state update to the same Interrupted UserCanceled error.

It's known issue for WebView2 downloading file. I found edge member has confirm this. We have a known issue where Downloads aren't working in UWP yet (due to file permissions). We are already working on the fix and should have it available for testing within the next month. I'm linking our existing work to this issue to track when it gets completed. Thanks! Please pay attention to subsequent updates.

See the progress on this issue here. This is the current process needed in order to get the downloads to work [as copied from above link]

  1. Install edge dev channel from - https://www.microsoftedgeinsider.com/en-us/download

  2. Note the installed path of that channel, currently - 'C:\Program Files (x86)\Microsoft\Edge Dev\Application\105.0.1300.0'

  3. Update UWP WebView2 package to latest prerelease version

  4. Set the following environment variables before app activation and webview initialization

     Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--edge-webview-optional-enable-uwp-regular-downloads");
    

and

Environment.SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", @"C:\Program Files (x86)\Microsoft\Edge Dev\Application\105.0.1300.0");
Related