c# webBrowser 网页下载不显示提示框 直接保存到指定文件夹

c# webBrowser 网页下载是,怎么不显示提示框,直接保存到指定文件夹?

img

img

在使用Webbrowser抓取网页信息时,碰到需要下载文件,这时需要用户介入操作,如何避免:

首先引进


```c#

[DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
          static extern Int32 URLDownloadToFile(
              [MarshalAs(UnmanagedType.IUnknown)] object pCaller,
              [MarshalAs(UnmanagedType.LPWStr)] string szURL,
              [MarshalAs(UnmanagedType.LPWStr)] string szFileName,
              Int32 dwReserved,
              IntPtr lpfnCB);

再添加  

```c#
this.WebBrowser.Navigating += WebBrowser_Navigating;

void WebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
           //设置下载条件
                  if (e.Url != null && e.Url.AbsolutePath.IndexOf("/V1/Delivery/Download") > -1)
                  {
                        try
                        {
                              if (Citation.IsValid)
                              {
                                    UIManager.ProgressMessageHandler(this, new UIEventArgs { Message = "Downloading "+ e.Url.Segments[e.Url.Segments.Length - 1]});
                     string fullname = Path.Combine(pdfSavePath_, e.Url.Segments[e.Url.Segments.Length - 1]);
                     
                     //在这替换“下载提示框”,并自定义下载
                     int response = URLDownloadToFile(null, e.Url.AbsoluteUri, fullname, 0, IntPtr.Zero);

                                    if (response == 0)
                                    {
                                          Citation.IsDownloaded = true;
                                          UIManager.ProgressMessageHandler(this, new UIEventArgs { Message = Citation.Name + " has downloaded." });
                                    }
                              }
                        }
                        catch
                        {
                        }
                        finally
                        {
                 //取消“下载提示框”,不会再提示用户
                              e.Cancel = true;
                        }
                  }
            }

通过 Internet Explorer 或要下载的文件的 web 浏览器应用程序通过调 c#里面重载 IDownloadManager 可以消除另存为和指定相应的路径。有帮助请采纳谢谢!

 public void Download(IMoniker pmk, IBindCtx pbc, UInt32 dwBindVerb, int grfBINDF, re IntPtr pBindInfo, String pszHeaders, String pszRedir, UInt32 uiCP)
 { 
string pwszUrl = null; 
pmk.GetDisplayName(pbc, null, out pwszUrl); 
//string s = StringToUnicode(pwszUrl);
 //string sFileYzmGif = null; 
//int iresult = URLDownloadToFile(null, pwszUrl, "d:\abc", 0, IntPtr.Zero); 
MessageBox.Show(pwszUrl); 
_amDBRSetThermoModel amf = (_amDBRSetThermoModel)GetFunctionAddress(hModule(), "DoFileDownload", typeof(_amDBRSetThermoModel)); 
int a = amf(pwszUrl);
 }

不要再用webbrowser了。
用了cef你才会发现你想实现的功能都能实现。

1.重写webBrowser控件;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebBorowserText
{
    public class MyWebBrowser : WebBrowser
    {
        /// <summary>
        /// The URLMON library contains this function, URLDownloadToFile, which is a way
        /// to download files without user prompts.  The ExecWB( _SAVEAS ) function always
        /// prompts the user, even if _DONTPROMPTUSER parameter is specified, for "internet
        /// security reasons".  This function gets around those reasons.
        /// </summary>
        /// <param name="callerPointer">Pointer to caller object (AX).</param>
        /// <param name="url">String of the URL.</param>
        /// <param name="filePathWithName">String of the destination filename/path.</param>
        /// <param name="reserved">[reserved].</param>
        /// <param name="callBack">A callback function to monitor progress or abort.</param>
        /// <returns>0 for okay.</returns>
        /// source: http://www.pinvoke.net/default.aspx/urlmon/URLDownloadToFile%20.html
        [DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern Int32 URLDownloadToFile(
            [MarshalAs(UnmanagedType.IUnknown)] object callerPointer,
            [MarshalAs(UnmanagedType.LPWStr)] string url,
            [MarshalAs(UnmanagedType.LPWStr)] string filePathWithName,
            Int32 reserved,
            IntPtr callBack);


        /// <summary>
        /// Download a file from the webpage and save it to the destination without promting the user
        /// </summary>
        /// <param name="url">the url with the file</param>
        /// <param name="destinationFullPathWithName">the absolut full path with the filename as destination</param>
        /// <returns></returns>
        public FileInfo DownloadFile(string url, string destinationFullPathWithName)
        {
            URLDownloadToFile(null, url, destinationFullPathWithName, 0, IntPtr.Zero);
            return new FileInfo(destinationFullPathWithName);
        }
    }
}

2.将MyWebBrowser控件,拖拽至你需要的窗体(Form),并在窗体加载事件(Load)添加以下代码;

 private void Form1_Load(object sender, EventArgs e)
        {
            myWebBrowser1.DownloadFile("http://crtcy.198424.com/excelpj.zip", "测试下载.zip");
        }

3.运行后打开项目bin\Debug文件夹,即可看到没有弹出便下载的文件测试下载.zip

img

需要源码的话,我可以发给你。