徐新帅 2017-04-05 10:08 采纳率: 100%
浏览 1497

使用Webbrowser控件 点击下载文件时,文件不能正确下载。求解

二、
namespace EasySoft.Webbrowser
{
///
/// 主要作用是屏蔽IFRAME 类的JS错误
///
public partial class WebbrowserExt : System.Windows.Forms.WebBrowser
{
public string BaseUrl = string.Empty;
public WebbrowserExt()
{
InitializeComponent();
this.AllowWebBrowserDrop = false;
(this.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += WebbrowserExt_NewWindow3;
(this.ActiveXInstance as SHDocVw.WebBrowser).FileDownload += WebbrowserExt_FileDownload;
}
public WebbrowserExt(IContainer container)
{
container.Add(this);
InitializeComponent();
this.AllowWebBrowserDrop = false;
var IhtmlDocument3 = (IHTMLDocument3)this.Document.DomDocument;
IhtmlDocument3.enableDownload = false;
(this.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += WebbrowserExt_NewWindow3;
(this.ActiveXInstance as SHDocVw.WebBrowser).FileDownload += WebbrowserExt_FileDownload;
}

    void WebbrowserExt_FileDownload(bool ActiveDocument, ref bool Cancel)
    {
        //取消默认的文件下载
        if (!ActiveDocument)
            Cancel = true;
    }

    void WebbrowserExt_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
    {
        BaseUrl = bstrUrl;
    }

    private SHDocVw.IWebBrowser2 Iwb2; 
    protected override void AttachInterfaces(object nativeActiveXObject)
    {
        Iwb2 = (SHDocVw.IWebBrowser2)nativeActiveXObject;
        Iwb2.Silent = true;
        base.AttachInterfaces(nativeActiveXObject);
    }

    protected override void DetachInterfaces()
    {
        Iwb2 = null;
        base.DetachInterfaces();
    }
}

}
一、
private void WebBrowser_NewWindow(object sender, CancelEventArgs e)
{
string strurl;
var wb = new WebbrowserExt();
wb.ScriptErrorsSuppressed = true;
wb.Navigating += new WebBrowserNavigatingEventHandler(WebBrowser_Navigating);
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
wb.NewWindow += new CancelEventHandler(WebBrowser_NewWindow);
wb.ObjectForScripting = this;
try
{
TabPage currentTabPage = tabControls.SelectedTab;
//WebBrowser myWebBrowser = (WebBrowser)currentTabPage.Controls[0];
var myWebBrowser = (WebbrowserExt)sender;
strurl = myWebBrowser.Document.ActiveElement.GetAttribute("href");
if (string.IsNullOrEmpty(strurl))
strurl = myWebBrowser.BaseUrl;
if (string.IsNullOrEmpty(strurl))
strurl = myWebBrowser.StatusText;
if (!strurl.Contains("://"))
strurl = "http://" + strurl;
//刷新页面,不使用缓存设置
if (strurl.Contains("?"))
strurl = strurl + "&t=" + DateTime.Now.Ticks;
else
strurl = strurl + "?t=" + DateTime.Now.Ticks;

if (strurl.Contains(".exe"))
{ }
else
{
wb.Navigate(strurl);
e.Cancel = true;
tabControls.TabPages.Add("新窗口" + SpaceString);
tabControls.TabPages[tabControls.TabPages.Count - 1].Controls.Add(wb);

                wb.Dock = DockStyle.Fill;

                tabControls.SelectedIndex = tabControls.TabPages.Count - 1;
                //清除缓存
                CleanTempFiles();
            }
        }
        catch (Exception e2)
        {
        }
    }

            点击类似为http://www.baidu.com/file/1234.doc, 不能下载成功。
            ![图片说明](https://img-ask.csdn.net/upload/201704/05/1491386913_512118.png)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB代码求涡量
    • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
    • ¥15 pointnet2包安装
    • ¥20 射频功率问题,解答者有酬谢!
    • ¥80 构建降雨和积水的预测模型
    • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
    • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
    • ¥100 github贡献者给与奖励
    • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
    • ¥15 odbc代码新增sqlserver数据源