Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET / ASP.NET4.0
Tip/Trick

File download from GridView rows in ASP.NET 4.0

2 May 2013CPOL 114.6K   17   19
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:
C#
<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>
   </ItemTemplate>
</asp:TemplateField>
  • Add one OnRowCommand="GridView1_RowCommand" event inside
    <asp:GridView tag.
In .cs page:
  • Define the event "GridView1_RowCommand" as follows:
C#
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)


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  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

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
Tom Marvolo Riddle29-Jan-14 19:19
professionalTom Marvolo Riddle29-Jan-14 19:19 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jan-14 19:21
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jan-14 19:21 
GeneralMy vote of 5 Pin
Debopam Pal18-Nov-13 15:32
professionalDebopam Pal18-Nov-13 15:32 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)18-Nov-13 18:58
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)18-Nov-13 18:58 
GeneralMy vote of 5 Pin
Michael Haephrati23-Feb-13 1:11
professionalMichael Haephrati23-Feb-13 1:11 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)23-Feb-13 1:20
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)23-Feb-13 1:20 
QuestionNice one Pin
Mr. Tapan Kumar20-Sep-12 2:04
Mr. Tapan Kumar20-Sep-12 2:04 
AnswerRe: Nice one Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)20-Sep-12 2:07
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)20-Sep-12 2:07 
GeneralMy vote of 5 Pin
Pro Idiot6-Aug-12 1:52
Pro Idiot6-Aug-12 1:52 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)6-Aug-12 1:53
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)6-Aug-12 1:53 
Questionhow can i view file with a hyper link? Pin
praveen_t_s30-Jun-12 20:39
praveen_t_s30-Jun-12 20:39 
GeneralRe: how can i view file with a hyper link? Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-Jul-12 21:54
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-Jul-12 21:54 
Questionhow do i add hyperlink to show a document from grid view? Pin
praveen_t_s30-Jun-12 19:59
praveen_t_s30-Jun-12 19:59 
AnswerRe: how do i add hyperlink to show a document from grid view? Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-Jul-12 20:43
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)3-Jul-12 20:43 
GeneralMy vote of 5 Pin
Srinivasaprabhu.N19-Apr-12 4:47
Srinivasaprabhu.N19-Apr-12 4:47 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)9-Jun-12 3:08
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)9-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.