One way of allowing the users to download a file is providing the direct link to the file. But if you want to do some custom processing before or after the file is downloaded, creating a custom handler for sending the file is more useful.Here is how I created the download button for allowing visitors to download the
txCaptcha plugin for wordpress.In my download button's click event handler.
string fn = Server.MapPath("~/path to the file..");Response.ClearHeaders();Response.ContentType = "application/x-force-download";Response.AddHeader("Content-Disposition", "attachment; FILENAME = \"txCaptcha.zip\"");Response.TransmitFile(fn);Response.End();
Here calling the response.End() is very important. If not called, the downloaded file will get corrupted as the rest of the output will also get appended to the file.The code is simple and hardcoded, but it should give you an idea about which HttpHeaders to send for forcing the browser to show the file download dialog.
Note: this code does not have support for resuming the download if it gets broken. So, if you are sending large file this simple method may not be the best method. I will be posting about how to implement support for resuming download in a later post.