• winform WebBrowser 文件下载拦截 获取文件本地保存路径


    这里需要引用 Interop.SHDocVw.dll

      1 public partial class WebBrowserForm : Form
      2     {
      3         WebClient client;
      4         SHDocVw.WebBrowser wb;
      5         private delegate bool IncreaseHandle(int nValue);
      6         //进度条窗口
      7         private ProcessBar myProcessBar = null;
      8         //声明委托对象
      9         private IncreaseHandle myIncrease = null;
     10 
     11         public WebBrowserForm()
     12         {
     13             InitializeComponent();
     14             wb = this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser;
     15             wb.FileDownload += new SHDocVw.DWebBrowserEvents2_FileDownloadEventHandler(wb_FileDownload);
     16             wb.NewWindow2 += new SHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(wb_NewWindow2);
     17         }
     18 
     19         private void btnShow_Click(object sender, EventArgs e)
     20         {
     21             try
     22             {
     23                 if (!String.IsNullOrEmpty(txtUrl.Text.Trim()))
     24                 {
     25                     Uri url = new Uri(txtUrl.Text.Trim());
     26                     webBrowser1.Url = url;
     27                 }
     28             }
     29             catch (Exception ex)
     30             {
     31                 MessageBox.Show(ex.Message);
     32             }
     33         }
     34         private void wb_FileDownload(bool ActiveDocument, ref bool Cancel)
     35         {
     36             MessageBox.Show("下载开始");
     37             client = new WebClient();
     38             var url = wb.Document.ActiveElement.GetAttribute("href");
     39             //判断是否为下载
     40             if (UrlFilter(url))
     41             {
     42                 SaveFileDialog save = new SaveFileDialog();
     43                 //save.InitialDirectory = Application.ExecutablePath;
     44                 save.Filter = "All Files(*.*)|*.*";
     45                 save.Title = "下载全文";
     46                 save.OverwritePrompt = true;
     47                 save.RestoreDirectory = true;
     48                 save.ValidateNames = true;
     49                 save.CheckPathExists = true;
     50                 save.AddExtension = true;
     51                 String fOldName = url.Split('/')[url.Split('/').Length - 1];
     52                 if (!String.IsNullOrEmpty(fOldName))
     53                 {
     54                     save.FileName = fOldName;
     55                 }
     56                 if (save.ShowDialog() == DialogResult.OK)
     57                 {
     58                     //使用线程起动
     59                     Thread thdSub = new Thread(new ThreadStart(ThreadFun));
     60                     thdSub.Start();
     61 
     62                     client.DownloadFile(url, save.FileName);
     63                 }
     64                 MessageBox.Show(save.FileName);
     65                 Cancel = true;
     66             }
     67         }
     68         private void wb_NewWindow2(ref object ppDisp, ref bool Cancel)
     69         {
     70             client = new WebClient();
     71             var url = wb.Document.ActiveElement.GetAttribute("href");
     72 
     73             if (UrlFilter(url))
     74             {
     75 
     76                 SaveFileDialog save = new SaveFileDialog();
     77                 //save.InitialDirectory = Application.ExecutablePath;
     78                 save.Filter = "All Files(*.*)|*.*";
     79                 save.Title = "下载全文";
     80                 save.OverwritePrompt = true;
     81                 save.RestoreDirectory = true;
     82                 save.ValidateNames = true;
     83                 save.CheckPathExists = true;
     84                 save.AddExtension = true;
     85                 String fOldName = url.Split('/')[url.Split('/').Length - 1];
     86                 if (!String.IsNullOrEmpty(fOldName))
     87                 {
     88                     save.FileName = fOldName;
     89                 }
     90                 if (save.ShowDialog() == DialogResult.OK)
     91                 {
     92                     //使用线程起动
     93                     Thread thdSub = new Thread(new ThreadStart(ThreadFun));
     94                     thdSub.Start();
     95                     client.DownloadFile(url, save.FileName);
     96                 }
     97                 MessageBox.Show(save.FileName);
     98                 Cancel = true;
     99             }
    100         }
    101 
    102         #region process bar
    103         private void ShowProcessBar()
    104         {
    105             myProcessBar = new ProcessBar();
    106             // 初始化进度条委托
    107             myIncrease = new IncreaseHandle(myProcessBar.Increase);
    108             myProcessBar.StartPosition = FormStartPosition.CenterParent;
    109             myProcessBar.ShowDialog();
    110             myProcessBar = null;
    111         }
    112         private void ThreadFun()
    113         {
    114             //线程中的一个委托
    115             MethodInvoker mi = new MethodInvoker(ShowProcessBar);
    116             this.BeginInvoke(mi);//异步挨靠委托
    117             //延时1秒显示窗口
    118             Thread.Sleep(1000);
    119 
    120             bool blnIncreased = false;
    121             object objReturn = null;
    122             //循环执行委托,直到返回值
    123             do
    124             {
    125                 Thread.Sleep(50);
    126                 objReturn = this.Invoke(this.myIncrease, new object[] { 2 });
    127                 blnIncreased = (bool)objReturn;
    128             }
    129             while (blnIncreased);
    130         }
    131         #endregion
    132 
    133         #region 过滤白名单
    134         public Boolean UrlFilter(String url)
    135         {
    136             Boolean result = false;
    137             url = url.ToLower();
    138             String[] filters = { ".rar"".zip"".pdf"".txt"".xls"".xlsx"".ppt"".doc"".docx" };
    139             foreach (String item in filters)
    140             {
    141                 if (url.EndsWith(item))
    142                 {
    143                     result = true;
    144                     break;
    145                 }
    146             }
    147             return result;
    148         }
    149         #endregion
    150     }

    前台用我们自己的System.Windows.Forms.WebBrowser.

  • 相关阅读:
    多阶段构建Docker镜像
    Docker容器跨主机通信
    数说海南——简单分析海南各市县近六年人口吸引力情况
    数说海南——透过几组数据简单分析近十年海南人口情况
    Kubernetes1.7—DNS安装
    BP神经网络
    Centos7——NFS(Network File System)服务
    Kafka中时间轮分析与Java实现
    Centos安装完成后,ifconfig:command not found
    Oracle VM VirtualBox虚拟机安装Centos
  • 原文地址:https://www.cnblogs.com/zhangpan1244/p/2825811.html
Copyright © 2020-2023  润新知