Web 浏览器关闭文件下载对话框

本文关键字:文件下载 对话框 浏览器 Web | 更新日期: 2023-09-27 18:31:42

我正在用Windows下载文件。Forms.WebBrowser c#,在我运行InvokeScript并获取我的文件后,我使用Application.DoEvents前进到我的下一个文件,但是下载对话框保持打开状态,我卡在Application.DoEvents上,什么也做不了。

如何取消或关闭此下载对话框?

我的代码:

     HtmlElement head = webBrowser.Document.GetElementById("btnExcelExport");
     HtmlElement scriptEl = webBrowser.Document.CreateElement("script");
     IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
     element.text = "function DownloadFile() {$('#btnExcelExport').click()}";
     head.AppendChild(scriptEl);
     webBrowser.Document.InvokeScript("DownloadFile");
     HtmlElementCollection iFrames =     webBrowser.Document.GetElementsByTagName("iframe");
                for (int i = 0; i < iFrames.Count; i++)
                {
                    string src = iFrames[i].GetAttribute("src");
                    if (src.Contains("Export"))
                    {
                        txt_url.Text = "https://test.com" + src;
                        string cooki = webBrowser.Document.Cookie;
                        int response = URLDownloadToFile(0, "https://test.com" + src, @"C:'temp'Test.txt", 0, 0);
                    }
                }
                Application.DoEvents();
//....got to next file

Web 浏览器关闭文件下载对话框

我知道

我的问题有问题,以及为什么我被降低率,但无论如何我找到了这个解决方案。

您需要使用导航事件,并在下载网址中使用取消下载"e.Cancel = true"检查您是否在下载网址中使用"e.Cancel = true"

private void webBrowser_Navigating(object sender,WebBrowserNavigatingEventArgs e)
        {
            if (e.Url.ToString().Contains("Download"))
            {
                e.Cancel = true;
            }
            }