Click here to Skip to main content
13,597,584 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 4 Oct 2013
Licenced CPOL

File Download on ImageButton or Button Click

, 4 Oct 2013
Rate this:
Please Sign up or sign in to vote.
File download on ImageButton or ButtonClick


In most of the web applications now a days, there comes a need when we have to provide a link to download some sample file or file like that as per the requirements. So this tip is to perform this task in the easiest way (which I have found so far, as I am a newbie.

Using the Code

The following code performs the file download process in the simplest way, either from the application's local folder or from some system directory.

The developer has to place these line of codes inside the buttonclick or imagebutton clickevent.

Response.ContentType = "Application/xlsx";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.xlsx");

In the first line, we have to mention the content type, I have used it for Excel 2007 files while here, we can use the PDF or image as well.

The second line is just appending the header to the httpResponse and here we have to mention the filename along with the extension which we want to download.

While in the third line Complete Path where file is placed has to be provided.

In the last line of code, the Response is ending.

Points of Interest

Many code snippets are available on Google, but I found this one pretty interesting and straight forward for any programmer who is a kind of newbie like myself.

Do give me your feedback, as this is my first effort on any forum in my life, either suggestions, corrections or praises. :) 


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


About the Author

Software Developer
Pakistan Pakistan
Working as an Associate Software Engineer in LMKT

Visit My Blog!

Contact Me!

You may also be interested in...


Comments and Discussions

GeneralJust i want to say thank you... Pin
Member 111978294-Feb-15 16:35
memberMember 111978294-Feb-15 16:35 
GeneralRe: Just i want to say thank you... Pin
VICK9-Feb-15 1:08
professional VICK9-Feb-15 1:08 
BugIt is good piece of code (when it comes to size) though not working for me. Pin
Pawan.riseup12-Jul-14 2:10
memberPawan.riseup12-Jul-14 2:10 
AnswerRe: It is good piece of code (when it comes to size) though not working for me. Pin
VICK13-Jul-14 18:17
professional VICK13-Jul-14 18:17 
GeneralRe: It is good piece of code (when it comes to size) though not working for me. Pin
Pawan.riseup8-Aug-14 2:40
memberPawan.riseup8-Aug-14 2:40 
AnswerRe: It is good piece of code (when it comes to size) though not working for me. Pin
VICK10-Aug-14 18:56
professional VICK10-Aug-14 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 4 Oct 2013
Article Copyright 2013 by VICK
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid