C# WebBrowser控件 下载文件不弹下载提示框的办法
参考链接 https://stackoverflow.com/questions/6773866/download-file-and-automatically-save-it-to-folder
public class MyWebBrowser : WebBrowser { ////// 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. /// /// Pointer to caller object (AX). /// String of the URL. /// String of the destination filename/path. /// [reserved]. /// A callback function to monitor progress or abort. /// 0 for okay. /// 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); /// /// Download a file from the webpage and save it to the destination without promting the user /// /// the url with the file /// the absolut full path with the filename as destination /// public FileInfo DownloadFile(string url, string destinationFullPathWithName) { URLDownloadToFile(null, url, destinationFullPathWithName, 0, IntPtr.Zero); return new FileInfo(destinationFullPathWithName); } }
然后使用这个MyWebBrowser控件代替.net自带的WebBrowser。
下载文件时使用
webBrowser1.DownloadFile(downloadUrl,"test.csv");