1. Add a LinkButton with Download Command Name
<asp:Label ID="lblFileID" runat="server" Text='<%# Eval("Fileid") %>'/>
<asp:LinkButton ID="lblFileName" CommandName="Download" runat="server" Text="Download" />
2. Implement the Gridview RowCommandEvent:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string strRequest = ((Label)row.FindControl("lblFileID")).Text;
if (strRequest != "")
{
string path = Server.MapPath(strRequest); //get file object as FileInfo
System.IO.FileInfo file = new System.IO.FileInfo(path); //-
if (file.Exists) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// write file to browser
Response.WriteFile(file.FullName);
Response.End();
}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}
else
{
//nothing in the URL as HTTP GET
Response.Write("Please provide a file to download.");
}
}
}
Done!