protected void grd_certificate_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("down") && e.CommandName !=null)
{
try
{
string fpath = e.CommandArgument != null ? "~/View/CertificationAgency/KIACertificate/FinalCertificates/" + Convert.ToString(e.CommandArgument) : null;
if (fpath != "")
{
if (fpath != "")
{
string path = MapPath(fpath);
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=" + System.IO.Path.GetFileNameWithoutExtension(fpath) + ".PDF");
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
}
}
}
catch (FileNotFoundException ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Nothing to download');", true);
}
catch (IOException ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Nothing to download');", true);
}
catch (Exception ex)
{
ExceptionHandler.WriteException(ex.Message);
}
}
}
--------------------------------
ASPX PAGE CODE
<asp:GridView ID="grd_certificate" AutoGenerateColumns="false" runat="server" CssClass="table table-bordered table-condensed table-hover table-responsive"
OnRowCommand="grd_certificate_RowCommand">
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton ID="lnk_download" runat="server" CommandName="down" CommandArgument='<%#Eval("certificatepath") %>'
Text="Download"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView
No comments:
Post a Comment