Click here to Skip to main content
13,196,946 members (52,568 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

20.3K views
Posted 4 Oct 2013

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

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.

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)

Share

About the Author

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


Visit My Blog!

Contact Me!

You may also be interested in...

Pro
Pro

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 
It's okay, the problem was with my browser or something, it is working fine. I was wondering if you could help me out in following scenario:
I want to redirect the page just after creating the pdf and let it be downloaded. If I use Repsonse.End() just after response.transmite() it does not let the page to redirect (that's obvious I guess), and I would not download if I use response.redirect() after response.transmite() and then response.end()
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 | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 4 Oct 2013
Article Copyright 2013 by VICK
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid