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 }
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.
在编程的道路上,一往无前,埋头狂奔。
[奔跑的人生] | [segmentfault] | [spring4all] | [csdn] | [掘金] | [OSChina] | [简书] | [知乎] | [51CTO]
[奔跑的人生] | [segmentfault] | [spring4all] | [csdn] | [掘金] | [OSChina] | [简书] | [知乎] | [51CTO]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?