954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

On-the-fly automated file download

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?

adamf07
Newbie Poster
7 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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
}
Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

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...

adamf07
Newbie Poster
7 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 
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.

adamf07
Newbie Poster
7 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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}");
					}
artin102
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You