使用web浏览器URL没有自动filedownload [英] Automated filedownload using WebBrowser without url
问题描述
我一直使用System.Windows.Forms.WebBrowser写在C#中的WebCrawler的。我试图下载文件时关闭一个网站,将其保存到本地计算机上。更重要的是,我想这是完全自动化。文件下载可以通过点击一个按钮,调用火花显示你想打开或保存此文件?对话框中选择下载JavaScript函数来启动。我绝对不希望手动点击另存为,并在文件名输入。
I've been working on a WebCrawler written in C# using System.Windows.Forms.WebBrowser. I am trying to download a file off a website and save it on a local machine. More importantly, I would like this to be fully automated. The file download can be started by clicking a button that calls a javascript function that sparks the download displaying a "Do you want to open or save this file?" dialog. I definitely do not want to be manually clicking "Save as", and typing in the file name.
我知道HttpWebRequest和WebClient的的下载功能,但由于在下载开始一个JavaScript,我不知道现在该文件的URL。据透露,javascript的是改变一些值,并提交形式doPostBack功能。
I am aware of HttpWebRequest and WebClient’s download functions, but since the download is started with a javascript, I do now know the URL of the file. Fyi, the javascript is a doPostBack function that changes some values and submits a form.
我试着在另存为对话框,web浏览器,而无需太多的成功它在那里获得自动对焦。我知道有一种方法强制下载保存,而不是要求增加一个头就可以了http请求保存或打开,但我不知道如何指定文件路径下载到。
I’ve tried getting focus on the save as dialog from WebBrowser to automate it from in there without much success. I know there’s a way to force the download to save instead of asking to save or open by adding a header to the http request, but I don’t know how to specify the filepath to download to.
推荐答案
我觉得你应该prevent甚至从显示下载对话框。这里可能是一个办法做到这一点:
I think you should prevent the download dialog from even showing. Here might be a way to do that:
-
JavaScript的code使您的WebBrowser控件导航到一个特定的URL(什么会导致出现下载对话框)
The Javascript code causes your WebBrowser control to navigate to a specific Url (what would cause the download dialog to appear)
要prevent从实际导航到以下链接WebBrowser控件,附加一个事件处理程序的导航事件。
To prevent the WebBrowser control from actually Navigating to this Url, attach a event handler to the Navigating event.
在您的导航事件你要分析,如果这是你想要停止(实际导航行动这一个下载网址,或许检查文件扩展名,必须有一个可识别的格式)。使用WebBrowserNavigatingEventArgs.Url这样做。
In your Navigating event you'd have to analyze if this is the actual Navigation action you'd want to stop (is this one the download url, perhaps check for a file extension, there must be a recognizable format). Use the WebBrowserNavigatingEventArgs.Url to do so.
如果这是正确的网址,通过设置WebBrowserNavigatingEventArgs.Cancel物业停止导航。
If this is the right Url, stop the Navigation by setting the WebBrowserNavigatingEventArgs.Cancel property.
继续下载自己用的HttpWebRequest或WebClient的类
Continue the download yourself with the HttpWebRequest or WebClient classes
看一看这个网页上的事件的详细信息:
<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx\">http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx
Have a look at this page for more info on the event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx
这篇关于使用web浏览器URL没有自动filedownload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!