2017-02-14 86 views
0

我有一個頁面將圖像上傳到SQL Server中的圖像列,並將其轉換爲二進制文件...在另一頁上我想要檢索圖像。我試圖在列表視圖中這樣做:ASP.NET - 將圖像數據從SQL Server轉換爲列表視圖中的圖像

<ItemTemplate> 
    <tr style=""> 
     <td> 
      <img src='data:image/jpg;base64,<%# Eval("Image") %>' /> 
     </td> 
     <td> 
      <asp:Label ID="ImageDescLabel" runat="server" Text='<%# Eval("ImageDesc") %>' /> 
     </td> 
    </tr> 
</ItemTemplate> 

我做錯了什麼?我認爲這將是解決這個問題的最好方法。

保存圖像的代碼如下所示:

void InsertImage() 
    { 
     byte[] theFile = new byte[FileUpload1.PostedFile.ContentLength]; 
     HttpPostedFile file = FileUpload1.PostedFile; 
     file.InputStream.Read(theFile, 0, (int)FileUpload1.PostedFile.ContentLength); 

     if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") 
     { 
      int result = new _Image() 
       { 
        ImageName = txtFileName.Text, 
        ImageFile = theFile, 
        ImageDesc = txtDescription.Text 
       }.AddImage(); 

      txtFileName.Text = ""; 
      txtDescription.Text = ""; 
     } 
    } 
+0

真正重要的是** ado.net **操作代碼。 –

+0

@雷洋我不確定我明白你的意思。 –

+0

我們怎樣才能談論數據庫沒有單行相關的代碼? –

回答

0

您試圖在這一行Eval("Image")使用byte[]代替base64 string

data:image/jpg;base64,<%# Eval("Image") %> 

將返回其不受html支持的byte array你你能Convert.ToBase64String(img) img應該是byte[]

+0

當我將圖像插入數據庫時​​,我會添加嗎?或之後?如果之後,你會把它放在哪裏? –

+0

這將是當你從數據庫中檢索後的圖像,因爲它會從二進制 – Usman

+0

如果你有問題,你可以粘貼SQL代碼數據retrival – Usman