如何在不使用文件路径

时间:2017-04-16 01:17:13

标签: c# asp.net gridview

我有一个模块将文件上传到我的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();
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分: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;
                    }
                }
            }