Download Attachment from Telerik Radgrid

Download Attachment  from Telerik Radgrid

  

Step :1 Design web form .aspx page

<div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

                <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" AllowPaging="true" PageSize="8" Width="50%" PagerStyle-AlwaysVisible="true"
                    GridLines="None" AutoGenerateColumns="false"
                    OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand">
                    <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                    <HeaderStyle HorizontalAlign="Left" />
                    <MasterTableView DataKeyNames="name" AutoGenerateColumns="false">
                        <NoRecordsTemplate>No records found.</NoRecordsTemplate>

                        <Columns>
                            <telerik:GridBoundColumn HeaderText="Name" DataField="Name" />
                            <telerik:GridBoundColumn HeaderText="Amount" DataField="Amount" />
                            <telerik:GridBoundColumn HeaderText="Phone" DataField="Phone" />
                            <telerik:GridBoundColumn HeaderText="PATH" DataField="pATH" />
                           
                           
                            <telerik:GridButtonColumn Text="Download" CommandName="download_file"></telerik:GridButtonColumn>

                        </Columns>


                    </MasterTableView>
                </telerik:RadGrid>
            </div>

Step :2 Logic On .CS page


    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Amount");
        dt.Columns.Add("Phone");
        dt.Columns.Add("pATH");


        dt.Rows.Add("Kandy", "12", "8802***2564", "honda_cbr_600rr_movistar-1280x800.jpg");
        dt.Rows.Add("Sam", "25", "545502*2544", "images (3).jpg");
        dt.Rows.Add("Izhar", "32", "95802*2452", "jg.jpg");
        dt.Rows.Add("Vineet", "10.2", "88022564***", "526225_169667016510697_388234992_n.jpg");

        RadGrid1.DataSource = dt;

    }
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "download_file")
        {

            GridDataItem ditem = (GridDataItem)e.Item;
            string filename = ditem["pATH"].Text;

            string path = MapPath("Images/" + 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();

        }
    }

It will look like :-



Comments

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click