首页 > 其他分享 >wpf webview2动态修改下载文件的下载路径 文件下载路径选择

wpf webview2动态修改下载文件的下载路径 文件下载路径选择

时间:2023-10-27 16:31:57浏览次数:37  
标签:文件 CoreWebView2 路径 saveFileDialog DownloadStarting deferral download 下载

通过webview2下载文件时候会将文件保存在用户的默认下载目录,
如果想调整成通过弹窗选择下载路径的方式则需要将默认行为做出修改。

本文通过CoreWebView2_DownloadStarting 这个事件来调整下载路径,
基本思路为通过弹窗让用户选择需要保存的路径,如果用户取消了此操作则通过这个事件提供的Handled句柄取消下载行为。

如果输入下载文件的路径、文件名后则只是修改此事件的目标文件名,而不影响其他逻辑。

在webview2控件的CoreWebView2InitializationCompleted事件中注入下载逻辑,另外注意需要使用System.Threading.SynchronizationContext.Current.Post执行,否则可能会导致
程序崩溃。

相关代码如下

private void webView2_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            webView2.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
        }

       private void CoreWebView2_DownloadStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2DownloadStartingEventArgs e)
        {
            // Developer can obtain a deferral for the event so that the CoreWebView2
            // doesn't examine the properties we set on the event args until
            // after the deferral completes asynchronously.
            CoreWebView2Deferral deferral = e.GetDeferral();

            // We avoid potential reentrancy from running a message loop in the download
            // starting event handler by showing our download dialog later when we
            // complete the deferral asynchronously.
            System.Threading.SynchronizationContext.Current.Post((_) =>
            {
                using (deferral)
                {
                    //string fileName = e.ResultFilePath;
                    //string downloadUrl = e.DownloadOperation.Uri;

                    // 弹出保存文件对话框,让用户选择保存路径和文件名
                    Trace.WriteLine("download starting");
                    var saveFileDialog = new System.Windows.Forms.SaveFileDialog();

                    saveFileDialog.Filter = "*.*|*.*";
                    saveFileDialog.FileName = e.ResultFilePath;
                    if (saveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        //取消默认下载的行为
                        e.Cancel = true;
                        return;
                    }
                    else
                    {
                        //重置下载路径后使用原webview行为继续下载
                        e.ResultFilePath = saveFileDialog.FileName;
                    }
                }
            }, null);
        }
private void webView2_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            webView2.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
            //webView2.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStartingTest;
        }

如果需要自己显示进度,可用通过DownloadStarting事件中CoreWebView2DownloadStartingEventArgs类型对象中的相关属性实现,

private void CoreWebView2_DownloadStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2DownloadStartingEventArgs e)
        {
            e.DownloadOperation.BytesReceivedChanged += Downloadoperation_BytesReceivedChanged; ; // subscribe to bytesreceivedchanged event
            e.DownloadOperation.EstimatedEndTimeChanged += Downloadoperation_EstimatedEndTimeChanged; ; // subsribe to estimatedendtimechanged event



            // Developer can obtain a deferral for the event so that the CoreWebView2
            // doesn't examine the properties we set on the event args until
            // after the deferral completes asynchronously.
            CoreWebView2Deferral deferral = e.GetDeferral();

            // We avoid potential reentrancy from running a message loop in the download
            // starting event handler by showing our download dialog later when we
            // complete the deferral asynchronously.
            System.Threading.SynchronizationContext.Current.Post((_) =>
            {
                using (deferral)
                {
                    //string fileName = e.ResultFilePath;
                    //string downloadUrl = e.DownloadOperation.Uri;

                    // 弹出保存文件对话框,让用户选择保存路径和文件名
                    Trace.WriteLine("download starting");
                    var saveFileDialog = new System.Windows.Forms.SaveFileDialog();

                    saveFileDialog.Filter = "*.*|*.*";
                    saveFileDialog.FileName = e.ResultFilePath;
                    if (saveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        //取消默认下载的行为
                        e.Cancel = true;
                        return;
                    }
                    else
                    {
                        //重置下载路径后使用原webview行为继续下载
                        e.ResultFilePath = saveFileDialog.FileName;
                    }
                }
            }, null);
        }
        private void Downloadoperation_EstimatedEndTimeChanged(object sender, object e)
        {
            if (sender is CoreWebView2DownloadOperation download)
            {
                var totalByte = download.TotalBytesToReceive;
                var receiveByte = download.TotalBytesToReceive;
            }
        }

        private void Downloadoperation_BytesReceivedChanged(object sender, object e)
        {

        }

另外需要注意给webview2控件进行销毁,例如在usercontrol的unloaded事件中处理

private void CommonWebContainer_Unloaded(object sender, RoutedEventArgs e)
        {
            webView2?.Dispose();
        }

留待后查,同时方便他人


标签:文件,CoreWebView2,路径,saveFileDialog,DownloadStarting,deferral,download,下载
From: https://blog.51cto.com/ives/8061288

相关文章

  • 实用小脚本——Windows系统使用dos命令删除文件
    @echooff&color0aset/pff=输入文件名:title查找%ff%echo.&set/p=正在查找<nulfor%%iin(cdefgh)do(ifexist%%i:\ (cd/d%%i:\set/p=%%i:<nulfor/f "delims="%%jin('dir/b/a-d/s"%ff%"2^>nul')do(echo.&......
  • mysql 导入csv 文件
    1.获取mysql配置文件路径mysqld--verbose--help|grep.cnf2.mysqld文件添加配置[mysqld]...secure-file-priv="" 3.建表createtablet_table(bank_codetext,banktext);4.导入csv文件BankCode,Bank(表头提前移除)ACEH,ACEHAGRONIAGA,Bank......
  • VBA获取文件夹下所有文件名或者文件夹名
    VBA获取文件夹下所有文件名或者文件夹名1,新建excel宏2,在sheet中添加宏执行按钮3,设置按钮执行的代码名VBA代码如下:`点击查看代码'选择文件按钮程序PrivateSubCommandButton1_Click()Application.ScreenUpdating=FalseCallChooseApplication.ScreenUpdatin......
  • linux系统中dtb文件的作用
    在Linux系统中,dtb文件是DeviceTreeBlob(设备树二进制)的缩写。它主要用于描述硬件配置和设备信息,为内核启动时加载驱动提供必要的参数。dtb文件可以减少内核版本的数量的原因在于,同一份Linux内核代码可以在多个板卡上运行,每个板卡可以使用自己的dtb文件。在Linux内核启动过程中会......
  • 大文件如何做断点续传?
    大文件如何做断点续传?上传大文件时,以下几个变量会影响我们的用户体验服务器处理数据的能力请求超时网络波动上传时间会变长,高频次文件上传失败,失败后又需要重新上传等等为了解决上述问题,我们需要对大文件上传单独处理1、分片上传分片上传,就是将所要上传的文件,按照一定的大小,将......
  • 大文件断点续传、快传秒传实现方案
    前言为什么视频、音频、大型文档等大文件不能也直接上传吗,简单又方便?遇到手动暂停、网络中断、网络不稳定或者服务端响应超时,当你终于半天到99%,网络突然断开喜提从0%再来一次再者一次服务接受如此大的数据传输,不说服务器肯同意接收,即使配置同意接受这常常会使服务器出现响应超时或......
  • 如何实现大文件断点续传、秒传
    大家先来了解一下几个概念:「文件分块」:将大文件拆分成小文件,将小文件上传\下载,最后再将小文件组装成大文件;「断点续传」:在文件分块的基础上,将每个小文件采用单独的线程进行上传\下载,如果碰到网络故障,可以从已经上传\下载的部分开始继续上传\下载未完成的部分,而没有必要从头开始上传......
  • 大文件分片上传和断点续传
    总结一下大文件分片上传和断点续传的问题。因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况。http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件切片(分块),但这不是我们现在说的重点,我们要做的事是保证在网络中断后1G的文件已上传的那......
  • 完整教程:使用SPRING BOOT实现大文件断点续传及文件校验
    一、简介随着互联网的快速发展,大文件的传输成为了互联网应用的重要组成部分。然而,由于网络不稳定等因素的影响,大文件的传输经常会出现中断的情况,这时需要重新传输,导致传输效率低下。为了解决这个问题,可以实现大文件的断点续传功能。断点续传功能可以在传输中断后继续传输,而不需要从......
  • 前端大文件分片上传断点续传
     分片上传分片上传是将大文件分成多个小文件进行上传,每个小文件的大小通常为1MB到10MB。上传时,将每个小文件分别上传到服务器,服务器再将这些小文件合并成一个完整的大文件。这种方法可以提高上传速度,减少上传失败的可能性。断点续传断点续传是指在上传过程中,如果上传失败或者中断......