【问题标题】:How to retireive image from database in gridview without using file path如何在不使用文件路径的情况下从gridview中的数据库中检索图像
【发布时间】:2017-04-16 01:17:13
【问题描述】:

我有一个模块可以将文件上传到我的 sqlserver 数据库中,其中我有 FName:varchar 用于文件名,ContentType:nvarchar 用于 FileType,Data:varbinary 用于实际数据。所以现在我可以使用 BoundField 在gridview中检索它,它只显示我上传的文件的名称,但我真正想要的是从数据库中识别文件,如果它是一个图像文件,那么它应该显示那个图像在gridview中,如果它是图像以外的文件,则让它只显示文件名。注意我不想使用文件夹来上传文件并使用文件路径检索它。有没有类似经历的可以分享一下:

<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="File Name"/>
        <asp:TemplateField ItemStyle-HorizontalAlign = "Center">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
                    CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后:

protected void Upload(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }

}

【问题讨论】:

  • 您的内容类型对吗?为什么不使用它来决定显示什么?
  • @Krishna 好的,我需要在 itemtemplate 中绑定/评估还是我该怎么做,你能准确吗?

标签: c# asp.net gridview


【解决方案1】:

您需要使用 ContentType 来决定如何显示数据

<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" OnRowDataBound="GridView1_RowDataBound"
    AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="File Name"/>
        <asp:TemplateField ItemStyle-HorizontalAlign = "Center">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile"
                    CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
                <img runat="server" id="imgData" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在行数据绑定中决定做什么

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton lnkButton = (LinkButton)e.Row.Cells[1].FindControl("lnkDownload");
                    HtmlImage img = (HtmlImage)e.Row.Cells[1].FindControl("imgData");
                    if (DataBinder.Eval(e.Row.DataItem, "ContentType").ToString().Contains("image"))//like image/jpeg
                    {
                        lnkButton.Visible = false;
                        img.Visible = true;
                        img.Src = "data:image/png;base64," + Convert.ToBase64String((byte[])DataBinder.Eval(e.Row.DataItem,"Data"));
                    }
                    else
                    {
                        lnkButton.Visible = true;
                        img.Visible = false;
                    }
                }
            }

【讨论】:

  • 它不显示图像也不抛出错误。下一步是什么?
  • @DharamRai 是否使图像在行数据绑定中可见?你调试了吗?发生了什么事?
  • 好的,它现在工作完美。感谢您分享您的学习成果。
猜你喜欢
  • 2011-08-20
  • 2016-03-14
  • 2019-01-05
  • 2014-02-28
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 2014-04-16
相关资源
最近更新 更多