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?

Dani AI

Generated

Short summary and what worked/failed so far: is correct that the WebBrowser control often only shows the script URL (not the final attachment) and that SendKeys / Win32 hacks can work but are brittle. ’s idea of intercepting navigation is sensible when the page issues a straight GET, and ’s SendKeys approach is the pragmatic fallback — but there are more robust options that avoid UI automation.

Capture-and-replay the real HTTP request (recommended first step). Use a proxy/devtools (Fiddler or the browser Network panel) to record the exact request that returns the file — method, URL (often a POST), headers, cookies, CSRF/token values and form body — then reproduce that same request from code (Python requests or .NET HttpClient) using a session/cookie container and stream the response to disk. This avoids the Save-As dialog entirely. (learn.microsoft.com)

If the filename or the file is created client-side (Blob, data: URI, or JS-generated link) or the page requires running complex JavaScript, use a headless browser that exposes download events. Playwright has a built-in download API that can wait for a download and save it programmatically; Selenium can also be made to bypass the prompt via the Chrome DevTools Protocol (Page.setDownloadBehavior). These run the JS and capture the final payload without SendKeys. Example (Playwright-style pseudo-usage shown):

with page.expect_download() as dl:
page.click("text=Download")
download = dl.value
download.save_as("out.xlsx")

(playwright.dev)

If staying with the WebBrowser/IE host, intercept the browser’s download handler instead of automating UI: implement the IDownloadManager callback for IE-hosted controls or host a Chromium embed (CefSharp) and use its download handler — both let the app receive the file stream and filename directly. (stackoverflow.com)

Troubleshooting checklist: capture cookies and auth headers, send Referer/User-Agent if required, follow redirects, stream the response to disk, and check Content-Disposition for the server filename. These steps eliminate the Save-As dialog in a reliable, automatable way.

Recommended Answers

All 6 Replies

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.