简体   繁体   English

如何在 UWP WebView2 上下载文件?

[英]How to download a file on a UWP WebView2?

I'm trying to support file downloads on a UWP WebView2.我正在尝试支持 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.不确定我是否做错了什么或者这是否是一个错误,但我不断将状态更改更新为中断,原因是 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" /> .我还尝试将 ResultFilePath 设置为 TemporaryFolder 或用户的 DownloadsFolder,并为应用程序提供了这些受限功能<rescap:Capability Name="broadFileSystemAccess" /><rescap:Capability Name="runFullTrust" />
But I consistently keep getting a single state update to the same Interrupted UserCanceled error.但是我一直不断地对同一个 Interrupted UserCanceled 错误进行单一状态更新。

Here's a sample project demonstrating the problem: https://github.com/nirbil/WebView2FileDownload这是一个演示问题的示例项目: https ://github.com/nirbil/WebView2FileDownload

Any ideas?有任何想法吗?

But I consistently keep getting a single state update to the same Interrupted UserCanceled error.但是我一直不断地对同一个 Interrupted UserCanceled 错误进行单一状态更新。

It's known issue for WebView2 downloading file. WebView2 下载文件的 已知问题 I found edge member has confirm this.我发现边缘成员已经确认了这一点。 We have a known issue where Downloads aren't working in UWP yet (due to file permissions).我们有一个已知问题,即下载在 UWP 中尚未运行(由于文件权限)。 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从 - 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'注意该通道的安装路径,当前 - 'C:\Program Files (x86)\Microsoft\Edge Dev\Application\105.0.1300.0'

  3. Update UWP WebView2 package to latest prerelease version将 UWP WebView2 包更新到最新的预发布版本

  4. Set the following environment variables before app activation and webview initialization在应用激活和 webview 初始化之前设置以下环境变量

     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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM