当前位置:首页 > Windows程序 > 正文

SetLastError = true )] static extern Int32 URLDownloadToFil

2024-03-31 Windows程序

参考链接 https://stackoverflow.com/questions/6773866/download-file-and-automatically-save-it-to-folder

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>Pointer to caller object (AX).</param> /// <param>String of the URL.</param> /// <param>String of the destination filename/path.</param> /// <param>[reserved].</param> /// <param>A callback function to monitor progress or abort.</param> /// <returns>0 for okay.</returns> /// source: %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>the url with the file</param> /// <param>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); } }

然后使用这个MyWebBrowser控件取代.net自带的WebBrowser。

下载文件时使用

webBrowser1.DownloadFile(downloadUrl,"test.csv");

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/35299.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494590访问次数
  • 建站天数
  • 友情链接