Download file in asp.net from gridview
.aspx code<asp:GridView ID="Grid_Circular_meeting" runat="server" AutoGenerateColumns="False"
CellPadding="4" Width="95%" EmptyDataText="No Record Exists!!!" AllowPaging="false"
ForeColor="#333333" GridLines="None" DataKeyNames="AutoId" OnRowCommand="Grid_Circular_meeting_RowCommand"
OnRowDeleting="Grid_Circular_meeting_RowDeleting" OnSelectedIndexChanged="Grid_Circular_meeting_SelectedIndexChanged">
<Columns>
<asp:BoundField HeaderText="Title" DataField="Title" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField HeaderText="MeetingDate" DataField="MeetingDate" DataFormatString="{0:dd-MMM-yyyy}"
HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField HeaderText="Location" DataField="Location" DataFormatString="{0:dd-MMM-yyyy}"
HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField HeaderText="MeetingTime" DataField="MeetingTime" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField HeaderText="File" DataField="FileCName" NullDisplayText="no file" />
<asp:BoundField HeaderText="FilePath" DataField="FilePath" Visible="false" />
<asp:TemplateField HeaderText="Show" HeaderStyle-HorizontalAlign="Left" Visible="true">
<ItemTemplate>
<asp:ImageButton ID="bthshow" runat="server" ImageUrl="~/Images/btnshow.jpg" CommandArgument='<%#Eval("FilePath") %>'
CommandName="download" ToolTip="show" Width="35px" Height="25px" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:ImageButton ID="btndelete" runat="server" ImageUrl="~/Images/close-button.gif"
CommandArgument='<%#Eval("AutoId") %>' CommandName="deleted" ToolTip="show" Width="35px" Height="25px" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" HorizontalAlign="Left" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
.CS Code
if (e.CommandName == "download")
{
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();
}
}
No comments:
Post a Comment