【发布时间】:2019-01-18 11:29:18
【问题描述】:
我试图在 GridView 中显示我上传的图片,但每次我想从数据库中获取数据并将字节数组再次解码为 base64 字符串时,我都会得到一个System.InvalidCastException。它表示对象System.String 无法转换为System.Byte[]。
以下是我目前编写的方法:
数据库中的图像表:
CREATE TABLE [dbo].[epadoc_mod_wound_image] (
[image_id] INT IDENTITY (1, 1) NOT NULL,
[image_file] NVARCHAR (MAX) NULL,
[file_size] VARCHAR (50) NULL,
[image_format] VARCHAR (100) NULL,
[image_time] DATETIME NULL,
[wound_id] INT NULL,
PRIMARY KEY CLUSTERED ([image_id] ASC),
FOREIGN KEY ([wound_id]) REFERENCES [dbo].[epadoc_mod_wound_details] ([wound_id])
上传方式:
protected void btn_Upload_Click(object sender, EventArgs e)
{
try
{
// checks, if the uploaded picture either is a jpeg- or a png-file
if (uploadWoundPic.PostedFile.ContentType == "image/jpeg" | uploadWoundPic.PostedFile.ContentType == "image/png")
{
// read the image properties
string imageFormat = uploadWoundPic.PostedFile.ContentType;
int fileSize = uploadWoundPic.PostedFile.ContentLength;
string imageSize = fileSize.ToString();
DateTime imageTime = DateTime.Now;
// convert the image to Byte Array
byte[] bytes = new byte[fileSize];
HttpPostedFile img = uploadWoundPic.PostedFile;
img.InputStream.Read(bytes, 0, fileSize);
// saves the filename in a variable
string filename = Path.GetFileName(uploadWoundPic.PostedFile.FileName);
// convert the Byte Array to Base64 Encoded string
string imageFile = Convert.ToBase64String(bytes, 0, bytes.Length);
// save picture on server in given folder
_db.SaveWoundImage(imageFile, imageSize, imageFormat, imageTime);
}
}
catch (Exception)
{
}
}
这没有问题。
带有图像的 GridView-Control:
<asp:GridView ID="woundImageGridview" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="wound_id" HeaderText="ID_Wunde" SortExpression="File"></asp:BoundField>
<asp:BoundField DataField="image_file" HeaderText="Imagefile" SortExpression="File"></asp:BoundField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="woundimage" runat="server" ImageUrl='<%# "data:image/png;base64," + Convert.ToBase64String((byte[])Eval("image_file"))%>' Height="150px" Width="150px"></asp:Image>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
以及我从数据库中获取数据的方法:
private void BindData()
{
ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["pflegedokumentationConnectionString"];
SqlConnection conn = new SqlConnection(connectionString.ConnectionString);
SqlCommand query = new SqlCommand("SELECT * FROM epadoc_mod_wound_image", conn);
conn.Open();
woundImageGridview.DataSource = query.ExecuteReader();
woundImageGridview.DataBind();
conn.Close();
}
但每次我调用该方法时,例如在 PageLoad 或按钮上单击时出现此异常。
【问题讨论】:
-
[image_file] NVARCHAR (MAX)?为什么不VARBINARY(MAX)? -
奇怪的是上传不适用于 varbinary(MAX),我可以点击上传,我没有收到异常,但图片没有存储在数据库中
-
你的意思是"doesn't work with varbinary(MAX)",你的意思是把
byte[]保存到DB?如果您不想或不能存储为varbinary,请将 base64 字符串保存在varbinary中,而不是byte[]。 -
在这一点上 ->
Convert.ToBase64String((byte[])他们都是准备好的字符串,而不是字节。 -
不能简单地将字符串转换为字节。您需要
byte[] bytes = Encoding.ASCII.GetBytes(someString);才能获取字节[],因为输入是来自数据库的字符串形式,然后将字节数组传递给转换
标签: c# asp.net sql-server database