2016-01-24 74 views
0

我正在用windows.Forms.WebBrowser c#下載文件,運行InvokeScript之後,並獲取我的文件我使用Application.DoEvents向前移動到我的下一個文件,但下載對話框保持打開狀態,我被卡在Application.DoEvents上,無法做任何事情。WebBrowser關閉文件下載對話框

如何取消或關閉此下載對話框?

我的代碼:

 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 

回答

0

我倒知道錯了我的問題,爲什麼我得到了利率,但無論如何,我發現這個解決方案。

你需要使用喲導航事件,有檢查的是你在下載的網址使用取消下載「e.Cancel =真正的」

private void webBrowser_Navigating(object sender,WebBrowserNavigatingEventArgs e) 
     { 

      if (e.Url.ToString().Contains("Download")) 
      { 
       e.Cancel = true; 
      } 
      }