C# 提交后下载网页内容
我需要自动从此站点下载文件 http://stats .smith.com/reports/Default.aspx?btnGo=查看+报告。我的问题是,一旦我单击提交按钮,我就会失去控制并弹出下载对话框。有没有办法使用 c# 下载文件并避免下载对话框?我当前正在 Forms 程序集中使用 WebBrowser 对象来浏览页面。
I need to automate the download of a file from this site http://stats.smith.com/reports/Default.aspx?btnGo=View+Report. My problem is once I click on the submit buttom I lose control and a download dialog pops up. Is there a way to download the file using c# and avoid the download dialog? I'm currently using the WebBrowser object in the Forms assembly to navigate through the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 WebClient< /a> 类
Take a look at the WebClient class
如果您想从网络浏览器将下载的文件保存到文件系统,则必须有用户交互。网页没有权限在客户端的文件系统中进行操作。
如果您想在浏览器中显示该页面,您可以尝试在下载文件时删除
Content-Disposition=attachment;...
服务器响应标头。仅当客户端将浏览器设置为在浏览器内显示此类文件类型时,这才有效。If you want to save a downloaded file to the filesystem from a web browser, there must be user interaction. A web page does not have permission to muck about in a client's file system.
If you want to display the page in the browser, you can try removing the
Content-Disposition=attachment;...
server response header when the file is downloaded. This will only work if the client has the browser set to display such file types inside the browser.您的问题没有指定您使用什么来下载该文件。
如果您询问是否可以有一个在客户端(WinForms 应用程序、控制台应用程序或 Windows 服务)上运行的程序,那么您可以使用 System.Net.WebClient 类并调用 DownloadFile() 方法。
接受的答案此处(稍微与您的问题不同,因此它不是重复的)有一个链接显示如何下载首先需要 HTTP post 的文件。
如果您尝试通过您托管的网页中的 JavaScript 以某种方式自动化 Internet Explorer,以强制用户下载文件而不显示对话框,那么不行。你不能。
Your question doesn't specify what you're using to download the file.
If you're asking if you can have a program that runs on a client (either a WinForms app, a console app, or a Windows Service) then you can download a file from a web site using the System.Net.WebClient class and calling the DownloadFile() method.
The accepted answer here (slightly different than you question, so it's not a duplicate) has a link to show how to download a file that requires an HTTP post first.
If you're trying to somehow automate Internet Explorer via a javascript from a web page you're hosting to force a file to download on a user without displaying the dialog box, then no. You can't.