asp.netentity-frameworktelerik-grid.net-4.8

Binary file doesn't get loaded in Radgrid


I'm loading data in a telerik:RadGrid, but I have one column that is not loading, which includes files (binary data).

As you can see in the image, instead of loading DB content, this column just loads System.Byte:

enter image description here

My current bound code is standard

<telerik:GridBoundColumn DataField="FileContent" 
         FilterControlAltText="Filter por conteudo de ficheiro"        
         HeaderText="Ficheiro">`

Any ideas on how to load the intended content?


Solution

  • I don't have the telrick gird. But we can display each row, and for each row say have a button on it. When you click on that button, it can downloads that bytes() column from the browser to the client computer.

    So, say we have this:

            <div style="width:40%">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="ID" CssClass="table" >
                    <Columns>
                        <asp:BoundField DataField="FileName" HeaderText="FileName"        />
                        <asp:BoundField DataField="MineType" HeaderText="MineType"        />
                        <asp:BoundField DataField="Description" HeaderText="Description"  />
    
                        <asp:TemplateField HeaderText="View">
                            <ItemTemplate>
                                <asp:ImageButton ID="cmdExcel" runat="server" Height="48px" Width="48px" 
                                    ImageUrl="~/Content/excel.png"
                                    OnClick="cmdExcel_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                </asp:GridView>
    

    Our database has a column called FileB (byes of the excel file).

    So, we can load up the grid, but NOT include the Excel file. But, we did place a button in the grid as per above.

    So, code to fill the grid can look like this:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadGrid();
        }
    
        void LoadGrid()
        {
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                string strSQL = "SELECT ID, FileName, MineType, Description FROM tblFiles";
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    GridView1.DataSource = cmdSQL.ExecuteReader();
                    GridView1.DataBind();
                }
            }
        }
    
        DataTable MyRst(string strSQL)
        {
            DataTable rst = new DataTable();
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
            {
                using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                {
                conn.Open();
                rst.Load(cmdSQL.ExecuteReader());
                }
            }
            return rst;
        }
    

    And now we have this:

    enter image description here

    Note in a above how we not only saved the file name, but ALSO saved the "mine" type. .net 4.5 (or later) has a built in function called GetMineType - you can pass it a file name, and it will produce the correct mine type.

    So, in above, when you click on the "image button", then we have this code to fetch the bytes from the database, and send it to the client:

      protected void cmdExcel_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
    
            GridViewRow gRow = (GridViewRow)btn.Parent.Parent;
            int PKID = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
    
            // get data from table
            DataRow rstData = MyRst("SELECT FileB, FileName, MineType from tblFiles where ID = " + PKID).Rows[0];
            Byte[] binFile = (Byte[])rstData["FileB"];
    
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
    
            Response.ContentType = rstData["MineType"].ToString();
            Response.AddHeader("Content-Disposition", "inline; filename=" + rstData["FileName"]);
    
            Response.BinaryWrite(binFile);
    
            Response.End();
    
        }
    

    As noted, most grids, be they gridview, list view or that telerick grid should work similar. So you don't include the bytes() data in the grid, but allow a button click, and stream down (send) the byte file to the client.