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!

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

8 thoughts on “How to make a file download on button click?”

  1. Imran,

    Nice post.

    I really appreciate if you can tell me how to automatically set the pdf file to full screen and the entire first page fits the full screen when the user clicks Open button (not Save As button)?

    Thanks!

    Bao

  2. Hi Bao,

    Thanks for dropping by. ah I’m not completely sure if I got your question. But I think what you want to do is to open the PDF file in full screen mode. Well basically both, save as and Open operations download the file to the client’s machine. The only difference is that the file goes into temporary files when you select the ‘Open’ command. And once you’re file is downloaded to the client’s machine, it normally uses the client end’s default program for opening PDF files — which would generally be Adobe Acrobat Reader. So I dont think you can control that with your server side code. However, what you can do is to not make it download like you’re doing in this article. You’d rather just give a link to the pdf file via Javascript. I’d rather open it in this kind of fashion:

    Open Full Screen Window

    and it should open the file named YourPdfFile.pdf in the browser and you can just open in it full screen mode in this way like I mentioned. Feel like wanting more on opening in Full Screen windows using Javascript? Have a go at these links here:

    http://javascript.internet.com/navigation/full-screen.html

    http://www.pptools.com/ppt2html/FAQ00140.htm

  3. Hey Imran,

    Your code has helped me a lot. Juat need a favour if you could help me in this. Please find the explanation below.

    1. On page I have few download links.
    2. on click of these link I open a popup window which has registration form.
    3. on button submit of the form i perform server side operation and also execute your code for download.
    4. After download is complete(open/save pdf ) I want to close the popup window automatically.
    5. I have written a javascript function which i call on body unload(). but it dosent work.
    6. Also i have tried it on onclientclick of button but then download process dosent work.

    Need help !!

  4. Salaam Shadab,

    Try using the ClientScript classes RegisterStartupScript method and in that you’d be giving the script to close the window — since this window gets opened through Javascript so browsers other than IE will have no problem in closing it as well. You should call this method right after you use my code to send the file via Response.

    Please let me know if this solves you’re problem.

    Best regards,

  5. salaam Imran,

    I have tried using ClientScript.RegisterClientScriptBlock Still could not achieve the result. i am putting the code with comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s