Click here to Skip to main content
15,880,469 members
Articles / Web Development / ASP.NET
Tip/Trick

File Download on ImageButton or Button Click

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
4 Oct 2013CPOL1 min read 40.6K   6
File download on ImageButton or ButtonClick

Introduction

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.

C#
Response.ContentType = "Application/xlsx";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.xlsx");
Response.TransmitFile(Server.MapPath("~/Templates/filename.xlsx"));
Response.End(); 

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. :) 

License

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


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


Visit My Blog!

Contact Me!

Comments and Discussions

 
GeneralJust i want to say thank you... Pin
Member 111978294-Feb-15 16:35
Member 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
Pawan.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
Pawan.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.