Click here to Skip to main content
12,249,797 members (59,048 online)
Click here to Skip to main content
Add your own
alternative version

Stats

113K views
17 bookmarked
Posted

File download from GridView rows in ASP.NET 4.0

, 2 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
There will be one Link button in the GridView row. On clicking the Link button, the Image displayed in that row gets downloaded.
In .aspx page:
  • Inside columns tag, add one template field like below:
<asp:TemplateField HeaderText="Screen Shot">
    <ItemTemplate>
         <asp:Image ID="screenShot" runat="server" ImageUrl='<%# Eval("screenshot") %>' CssClass="image" AlternateText="No Screenshots available"/>
         <asp:LinkButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("screenshot") %>' CommandName="cmd">Download</asp:LinkButton>
  • Add one OnRowCommand="GridView1_RowCommand" event inside
    <asp:GridView tag.
In .cs page:
  • Define the event "GridView1_RowCommand" as follows:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
 
   if (e.CommandName == "cmd")
   {
      string filename = e.CommandArgument.ToString();
      if (filename != "")
      {
         string path = MapPath(filename);
         byte[] bts = System.IO.File.ReadAllBytes(path);
         Response.Clear();
         Response.ClearHeaders();
         Response.AddHeader("Content-Type", "Application/octet-stream");
         Response.AddHeader("Content-Length", bts.Length.ToString());
 
         Response.AddHeader("Content-Disposition", "attachment;   filename=" + filename);
 
         Response.BinaryWrite(bts);
 
         Response.Flush();
 
         Response.End();
      }
   } 
}

Important Points

  • Example is given for Image download, but you can download files of any type by just giving the path in "CommandArgument" attribute
  • Q: What is ImageUrl='' ???
  • A: It is written to specify/evaluate the path of image that we got from dataset(column name in dataset is "screenshot").
  • Q: What is CommandArgument='' ???
  • A: Again CommandArgument takes the ImageUrl which is obtained from the dataset.

License

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

Share

About the Author

Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Software Developer Mindfire Solutions
India India
NOW I am a Conference Speaker. You can invite me to your College, Company, Event or Conference !!!

Proud Indian | Microsoft MVP | CodeProject MVP | Author | DZone Most Valuable Blogger | Community Expert | Moderator | jsfiddler | Cricketer | Cook | Dancer

Currently working @Mindfire Solutions.
Love to code and help guys to Trap the Bug.

My Tech Blog

taditdash.wordpress.com

Programming Community Profiles

jsfiddle | Stack Overflow

Other Blog Profiles

Mindfire CRM Team Blog | Mindfire Blog

Social Profiles

Facebook | Twitter | LinkedIn | Google+

Awards

  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sibeesh KV13-Nov-14 3:14
professionalSibeesh KV13-Nov-14 3:14 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Nov-14 3:29
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Nov-14 3:29 
GeneralRe: My vote of 5 Pin
Sibeesh KV13-Nov-14 3:35
professionalSibeesh KV13-Nov-14 3:35 
GeneralMy vote of 5 Pin
Jas2429-Jan-14 19:19
professionalJas2429-Jan-14 19:19 
GeneralRe: My vote of 5 Pin
Tadit Dash29-Jan-14 19:21
mvpTadit Dash29-Jan-14 19:21 
GeneralMy vote of 5 Pin
Debopam Pal18-Nov-13 15:32
memberDebopam Pal18-Nov-13 15:32 
GeneralRe: My vote of 5 Pin
Tadit Dash18-Nov-13 18:58
professionalTadit Dash18-Nov-13 18:58 
GeneralMy vote of 5 Pin
Michael Haephrati23-Feb-13 1:11
mvpMichael Haephrati23-Feb-13 1:11 
GeneralRe: My vote of 5 Pin
Tadit Dash23-Feb-13 1:20
memberTadit Dash23-Feb-13 1:20 
QuestionNice one Pin
K.Tapan Kumar20-Sep-12 2:04
memberK.Tapan Kumar20-Sep-12 2:04 
AnswerRe: Nice one Pin
Tadit Dash20-Sep-12 2:07
memberTadit Dash20-Sep-12 2:07 
GeneralMy vote of 5 Pin
Deepak Jena6-Aug-12 1:52
memberDeepak Jena6-Aug-12 1:52 
GeneralRe: My vote of 5 Pin
Tadit Dash6-Aug-12 1:53
memberTadit Dash6-Aug-12 1:53 
Questionhow can i view file with a hyper link? Pin
praveen_t_s30-Jun-12 20:39
memberpraveen_t_s30-Jun-12 20:39 
GeneralRe: how can i view file with a hyper link? Pin
Tadit Dash3-Jul-12 21:54
memberTadit Dash3-Jul-12 21:54 
Questionhow do i add hyperlink to show a document from grid view? Pin
praveen_t_s30-Jun-12 19:59
memberpraveen_t_s30-Jun-12 19:59 
AnswerRe: how do i add hyperlink to show a document from grid view? Pin
Tadit Dash3-Jul-12 20:43
memberTadit Dash3-Jul-12 20:43 
GeneralMy vote of 5 Pin
Srinivasaprabhu.N19-Apr-12 4:47
memberSrinivasaprabhu.N19-Apr-12 4:47 
GeneralRe: My vote of 5 Pin
Tadit Dash9-Jun-12 3:08
memberTadit Dash9-Jun-12 3:08 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 2 May 2013
Article Copyright 2012 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid