簡體   English   中英

ASP.NET C#將圖像從數據庫綁定到GridView

[英]ASP.NET C# Binding Images to GridView from Database

我試圖在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或按鈕上調用該方法時,都會得到此異常。

信息很清楚

無法將System.String轉換為System.Byte []

在那時候

<asp:Image ID="woundimage" runat="server" ImageUrl='<%# "data:image/png;base64," 
 + Convert.ToBase64String((byte[])Eval("image_file"))%>' Height="150px" Width="150px"></asp:Image>

Eval("image_file")

是一個字符串,而不是一個byte [],聲明是字符串,保存到數據庫中的是字符串-因此這是主要錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM