Cha*_*ani 6 c# windows webview
我设计了Windows窗体应用程序,在其中实现了Webview。我正在使用 webview.Navigate 导航到不同的 Url。导航后,我正在从该网址下载文件。文件下载成功,但正在默认位置下载,即C:\Users\XYZ\Downloads。
我想更改webview的默认下载位置。我们可以更改webview的默认下载位置吗?或者有没有其他方法可以从 webview 的不同位置下载。
谢谢您的帮助
小智 2
假设您的应用程序使用以下或更高版本的 nuget WebView2包:
安装包 Microsoft.Web.WebView2 -版本 1.0.865-预发布
为了管理下载进度,您可以尝试以下代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private async void Form1_Load(object sender, EventArgs e)
{
BirWV.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
await InitializeAsync(BirWV);
BirWV.CoreWebView2.Navigate("https//myurl.com");
}
private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
downloadOperation = e.DownloadOperation;
e.ResultFilePath = @"C:\mytargetdowloadpath\mydownloadedfile.zip";
}
public async Task InitializeAsync(WebView2 wv, string webCacheDir = "")
{
CoreWebView2EnvironmentOptions options = null;
string tempWebCacheDir = string.Empty;
CoreWebView2Environment webView2Environment = null;
tempWebCacheDir = webCacheDir;
if (String.IsNullOrEmpty(tempWebCacheDir))
{
tempWebCacheDir = System.IO.Path.GetTempPath();
tempWebCacheDir = System.IO.Path.Combine(tempWebCacheDir, System.Guid.NewGuid().ToString("N"));
}
webView2Environment = await CoreWebView2Environment.CreateAsync(null, tempWebCacheDir, options);
await wv.EnsureCoreWebView2Async(webView2Environment);
var initTaskField = BirWV.GetType().GetField("_initTask", BindingFlags.Instance | BindingFlags.NonPublic);
dynamic initTask = initTaskField.GetValue(wv);
BirWV.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
}
private void WebView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
请检查在调试模式下运行后是否达到了WebView_CoreWebView2InitializationCompleted方法。
| 归档时间: |
|
| 查看次数: |
2998 次 |
| 最近记录: |