My problem is this: I am trying to automate the download, parsing, and insertion of an excel file into a database. I have the parser and inserter done, but I can't seem to find a way to download the files automatically.

The core of the issue is that the website seems to generate the file names on the fly. So far, we have been trying to use a WebBrowser control to access the website, log in, and run the website's download script, at which point a 'save file' dialog box pops up with the file we want to save. This, however, requires human input which is the exact opposite of what automation is all about.

So, my question is this: is there a better way to do this? And if not, is there a way to access that dialog box and get the file from it?

You should override the Navigating event of the browser control. You're going to have to figure out when the URL is the download url (the file name might change, but it might be in a certain path or something), and cancel the navigation, then download the url with the WebClient class.

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
       //e.Url is the URL of the request
       //e.Cancel = true; will cancel the request in the web browser so you can handle it on your own
}

Thank you for the reply.

I've already tried this, but it doesn't give me the URL of the file, just the URL of the script that is sending me the file. At this point I'm looking at using wsh to automatically start the download using sendkeys and the like. It feels like a cop-out though...

So what exactly initiates the download? It comes from javascript? You might have to just parse the javascript. Sendkeys would be a cop out :)

So what exactly initiates the download? It comes from javascript? You might have to just parse the javascript. Sendkeys would be a cop out :)

I agree wholeheartedly, but I spent half of yesterday and half of today doing research and it seems that this is the only way to get what I need done. I've found a way to do it, using the win32 API. It's not pretty or elegant, but it gets the job done.

Thanks for the help.

try something like this. I send the key strokes using SendKeys.SendWait... to put in a file name for an upload control.
so look for the DocumentCompleted event once it does that or you click the link to get the file just set a timer to like 5 sec. then send the string and then your done.

path_file = (HtmlElement)getElement(myDoc, "path", "name");
					if ((string)path_file.GetAttribute("type") == "file")
					{

						path_file.Focus();
						filenameArray = fld_fileName.Split('\\');
						for (int i = 0; i < 5000; i++)
						{
						}
						SendKeys.SendWait("      " + fld_fileName + "{ENTER}");
					}

Hi adamf07,

how you have achieved your requirement using win32 API.
Please tell me,i'm tired by searching ..not found any solution on google

Thanks,
Juhi

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.