Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im stuck, I been looking for an answer for days now and I cant find any answer that fits my requirements. I dont know how to do it. I dont have any code yet since I dont know if this is possible.

I have a user access on the website then there is a button that once I click the file it will download, and the save dialog box will display asking for a folder location. The file doesnt have a link or an address. I think its created in the backend once the excel button is click.

Now my question is, how can I download the excel file after I click the button excel without displaying the save dialog box using windows form web browser? Or if that’s not possible. How can I click the save button automatically on the save dialog box then automate the folder location.

What I have tried:

I tried researching and I still cant find the right code for my desktop app
Updated 7-Mar-18 1:52am
F-ES Sitecore 6-Mar-18 8:26am    
Are you automating a web browser control inside your desktop app? The only way you'll probably get this working is if you don't use the web browser control at all but instead request the file yourself using something like WebClient

Obviously if that is possible depends entirely on the site you are automatig, *if* you're automating a web site, and a million other important things you haven't mentioned.
Cey P 7-Mar-18 3:53am    
I’m automating the download excel file. Everytime I click the excel button the dialog box showed up. How can I disable it? If the web browser is not possible, then where should I do it so that the save dialog box will not show up but still will save the file on my local drive. By the way, the excel doesn’t have link.
F-ES Sitecore 7-Mar-18 4:02am    
How easy this is all depends on the page you're automating, you can't force a download from a browser, that's never going to work. Instead your code is going to have to act like a browser and send the same requests to the same site and process the response in a way you want (ie save straight to disc rather than what the browser wants to do). I'd start by using something like Fiddler or the browser dev tools to see exactly what request is sent when you click the button and with what POST data, and emulate that using HttpRequest or WebClient.
Cey P 7-Mar-18 5:07am    
When I right click or inspect element the button, it display the image of the excel button icon. It doesnt show the path or anything, it just the images of the excel icon. I can download the file by using the webbrowser.document.getelememtid(“button id name”) then used the webbrowser1.invokemember(“click”) but the save dialog box showed up. What should I do? Since the excel doesnt have url address. I need to automate the downloading of the excel file.
F-ES Sitecore 7-Mar-18 5:14am    
You need to emulate what the browser is doing. When you click the button the browser makes a request to the server that gives the response which is the file to download. What you need to do is find out what that request looks like (using Fiddler, google it, it's a free app) and send the same request using code (via WebClient etc) such that you get the same response back. Forget the webbrowser control, you're never going to get what you want using that.

1 solution

If my download page is like this;

<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click" />


protected void btnDownload_Click(object sender, EventArgs e)
    string path = "~/files/pic.png";
    Response.Buffer = true;
    Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
    Response.ContentType = "application/octet-stream";

So clicking the download button causes the code to write the files/pic.png file to the client and it triggers a download in the browser. If I look at the request in Fiddler it is to this url


and the post data is like this;


That's the form values on the page. The important bit of post data is the bit that tells that the download button has been clicked, that's this


If I view the source of my page I see my download button is called "ctl00$MainContent$btnDownload" (it's using master pages ergo the weird name). So I want to POST to that url with that in the post data and that will make think the request has come from a browser clicking the download button, so I just grab the response and save it to a file;

string url = "http://localhost:50725/Default.aspx";

NameValueCollection data = new NameValueCollection();
// data.Add("__EVENTTARGET", ""); // you can add any post data as needed
data.Add("ctl00$MainContent$btnDownload", "Download"); // this simulates the button click

using (WebClient client = new WebClient())
    byte[] result = client.UploadValues(url, data); // UploadValues triggers a POST
    File.WriteAllBytes(@"c:\temp\download.png", result); // save the response to the temp folder

I'm using a png file but it doesn't matter what the file is, this will work for Excel files, EXE files, any binary data.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900