我没有telrick gird。但是我们可以显示每一行,并且每行都有一个按钮。当您单击该按钮时,它可以将该 bytes() 列从浏览器下载到客户端计算机。
所以,假设我们有这个:
<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>
我们的数据库有一个名为 FileB 的列(excel 文件的再见)。
因此,我们可以加载网格,但不包括 Excel 文件。但是,我们确实按照上面的方法在网格中放置了一个按钮。
因此,填充网格的代码如下所示:
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;
}
现在我们有了这个:
请注意,我们不仅保存了文件名,还保存了“我的”类型。 .net 4.5(或更高版本)有一个名为 GetMineType 的内置函数 - 您可以将文件名传递给它,它会生成正确的地雷类型。
所以,在上面,当你点击“图像按钮”时,我们有这段代码从数据库中获取字节,并将其发送给客户端:
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();
}
如前所述,大多数网格,无论是网格视图、列表视图还是 Telerick 网格都应该类似地工作。因此,您不会在网格中包含 bytes() 数据,而是允许单击按钮,并将字节文件向下(发送)到客户端。