gpt4 book ai didi

asp.net - System.Byte[] 在gridview 中显示而不是图像?

转载 作者:行者123 更新时间:2023-12-04 23:05:09 25 4
gpt4 key购买 nike

嗨,我有一个页面可以让我上传照片。然后当我点击上传...照片已被批准。但是当我想在gridview中显示它时。图像必须出现的列给了我“System.Byte []”而不是图像。帮助 :)

这是我的代码:

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>
<%@ Import Namespace= "System.Data.OleDb" %>


<script runat="server" type="text/c#">
protected void btnUpload_Click(object sender, EventArgs e)


{
{
byte[] imageSize = new byte
[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
uploadedImage.InputStream.Read
(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
// Create SQL Connection
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["recordsConnectionString"].ConnectionString;
// Create SQL Command
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "INSERT INTO PendingRecords([Image])" + " VALUES (@Image)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;

OleDbParameter UploadedImage = new OleDbParameter
("@Image", OleDbType.VarBinary, imageSize.Length);

UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
con.Open();
cmd.ExecuteNonQuery();
GridView1.DataSourceID = "";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
con.Close();
}
}
</script>

<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Label ID="lblMessage" runat="server">
</asp:Label>
<asp:Button ID="btnUpload" runat="server"
OnClick="btnUpload_Click" Text="Upload"/>
</div>
<asp:GridView runat="server" id="GridView1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="ID">
<Columns>
<asp:boundfield DataField="ID" HeaderText="ID" ReadOnly="True" InsertVisible="False" SortExpression="ID">
</asp:boundfield>
<asp:boundfield DataField="Image" DataFormatString="{0}" HeaderText="Image" SortExpression="Image">
</asp:boundfield>
<asp:boundfield DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName">
</asp:boundfield>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="<%$ ConnectionStrings:recordsConnectionString.ProviderName %>" ConnectionString="<%$ ConnectionStrings:recordsConnectionString %>" SelectCommand="SELECT * FROM [PendingRecords]" DeleteCommand="DELETE FROM [PendingRecords] WHERE [ID] = ?" InsertCommand="INSERT INTO [PendingRecords] ([Username], [Password], [FirstName], [LastName], [MiddleName], [Address], [Gender], [ContactNumber], [PlateNumber], [Color], [Brand], [LiscensedNumber]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [PendingRecords] SET [Username] = ?, [Password] = ?, [FirstName] = ?, [LastName] = ?, [MiddleName] = ?, [Address] = ?, [Gender] = ?, [ContactNumber] = ?, [PlateNumber] = ?, [Color] = ?, [Brand] = ?, [LiscensedNumber] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="Username" Type="String" />
<asp:parameter Name="Password" Type="String" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="ContactNumber" Type="String" />
<asp:parameter Name="PlateNumber" Type="String" />
<asp:parameter Name="Color" Type="String" />
<asp:parameter Name="Brand" Type="String" />
<asp:parameter Name="LiscensedNumber" Type="String" />
<asp:parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="Username" Type="String" />
<asp:parameter Name="Password" Type="String" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="ContactNumber" Type="String" />
<asp:parameter Name="PlateNumber" Type="String" />
<asp:parameter Name="Color" Type="String" />
<asp:parameter Name="Brand" Type="String" />
<asp:parameter Name="LiscensedNumber" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</form>

最佳答案

我可能会为您提供一个不需要太多更改的小解决方案。恕我直言,有点乱。

将您的图像绑定(bind)字段替换为下面的此模板字段。

   <asp:TemplateField>
<HeaderTemplate>Image</HeaderTemplate>
<ItemTemplate>
<img src='data:image/jpg;base64,<%# Eval("Image") != System.DBNull.Value ? Convert.ToBase64String((byte[])Eval("Image")) : string.Empty %>' alt="image" height="100" width="200"/>
</ItemTemplate>
</asp:TemplateField>

关于asp.net - System.Byte[] 在gridview 中显示而不是图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14211292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com