2016-09-24 57 views
0

不幸的是,我不能使用repater和泛型處理程序顯示我的圖像,我稱之爲ImageHandler.ashx,可能我做錯了什麼,而且我自己也找不到錯誤。圖像不顯示ASP.NET(通用handler.ashx)

這是我的代碼(順便說一句,這是我第一次使用這個通用處理程序結合中繼器從數據庫中獲取我的圖像,我被卡在這裏)。

這裏是我的代碼:

<table class="table table-hover" style="margin-top: 50px; background-color: white;"> 
<tr> 
    <th>Name</th> 
    <th>Last Name</th> 
    <th>Photo</th> 
</tr> 
<asp:Repeater ID="repPlayers" runat="server" OnItemCommand="repPlayers_ItemCommand"> 
    <ItemTemplate> 
     <tr> 

      <td><%# Eval("Name") %></td> 
      <td><%# Eval("LastName") %></td> 
      <td> 
       <asp:Image ID="imageTest" src="~/ImageHandler.ashx?id=PlayerID" runat="server" /> 
       <%--<img id="imageTest" style="width: 60px; height: 30px;" src="data:image/png;base64, <%# Convert.ToBase64String((byte[])Eval("Photo")) %> " />--%> 
      </td> 

     </tr> 
    </ItemTemplate> 
</asp:Repeater> 

ImageHandler.ashx後面的代碼:

public class ImageHandler : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 

     int playerID = Convert.ToInt32(context.Request["PlayerID"]); 
     Players.Data.DataAccess player = Players.Data.DataAccess.GetByPlayerId(playerID); 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.BinaryWrite(player.Photo); 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

也許我做錯了什麼,也許我應該結合'<%#Eval("PlayerID")%>'不知何故或什麼? 如果我使用DataGrid的,我會用Item_dataBound控制和可以解決myproblem但不幸的是我在這裏我加載到數據表中,我不知道如何解決這個..

編輯: 我想這也和這是解決方案: <asp:Image ID="imageTest" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?PlayerID="+Eval("PlayerID") %>' />

PS註釋代碼的作品,但它是緩慢的加載圖像的地獄,這讓我的整個頁面加載速度很慢,因爲我越來越喜歡從數據庫50個對象和他們每個人都有像:(

因爲我發佈上述解決方案,我們可以威脅到這一點!

+0

在您設置它的MIME類型爲image/PNG,但在你的處理器,你將它設置爲圖像/ JPEG註釋代碼。你所有的圖像是PNG還是與JPEG(可能還有其他格式)混合? –

+0

現在所有的都是.jpg,,正如我所說的那樣,代碼可以很好地處理圖像,但是它很慢:/ –

+0

這是什麼意思「慢」?你如何測試它?針對單獨的Windows Server或針對您的開發Windows 7/10? –

回答

0

//這裏是我的編輯,使這個工程:

<td> 
    <asp:Image ID="imageTest" runat="server" style="width: 60px; height: 30px;" 
     ImageUrl='<%#"~/ImageHandler.ashx?PlayerID="+Eval("PlayerID") %>' /> 
</td>