How to make a file download on button click?
Ever wonder how you could put in the functionality of downloading a file when the user clicks the button? Well here’s how I did it. The technique is to send the file to the browser via response and put the content type to application/pdf. Without putting in any further delays, here’s the code.
/// <summary>
/// opens the specified file in the request when provided the correct content type
/// </summary>
/// <param name=”FileName”></param>
/// <param name=”ContentType”></param>
/// <param name=”SaveAsFileName”></param>
/// <param name=”response”></param>
public static void ResponseOpenFile(string FileName, string ContentType, string SaveAsFileName, HttpResponse response)
{
response.Clear();
response.AddHeader(“content-disposition”, “attachment;filename=” + SaveAsFileName);
response.ContentType = ContentType;
response.WriteFile(FileName);
response.End();
}
Thats how I call the method. This is being done in the on click event of the download button.
ResponseOpenFile(Server.MapPath(“../applications/”)+oApp[0].ApplicationID.ToString()+”.pdf”, “application/pdf”, “Application.pdf”, Response);
Happy Programming folks!

