Monday, May 2, 2022

File download docx OR XML file in ASP.NET C#

 protected void lnkDownloadAnnexB_Click(object sender, EventArgs e)

    {

        try

        {

            Response.ContentType = "application/docx";


            string filePath = "~/Document/Download/ANNEXURE-B.docx";


            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");

            Response.TransmitFile(Server.MapPath(filePath));

            Response.End();

        }

        catch (Exception)

        {

        }

    }


OR


protected void imgButton_Click(object sender, ImageClickEventArgs e)

    {

        try

        {

            Response.ClearContent();

            Response.Buffer = true;

            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "DetailsReport.xls"));

            Response.ContentType = "application/ms-excel";

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);


            gvCourseList.AllowPaging = false;


            //Change the Header Row back to white color

            gvCourseList.HeaderRow.Style.Add("background-color", "#FFFFFF");

            //Applying stlye to gridview header cells

            for (int i = 0; i < gvCourseList.HeaderRow.Cells.Count; i++)

            {

                gvCourseList.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");

            }

            gvCourseList.RenderControl(htw);

            Response.Write(sw.ToString());

            Response.End();

        }

        catch (Exception ex)

        {

        }



    }

No comments:

Post a Comment