簡體   English   中英

從db和OutputStream異常顯示圖像ASP.NET/C#

[英]Displaying image from db & OutputStream Exceptions ASP.NET/C#

晚安,晚上等等等^ _ ^

嘗試以asp:Image(使用Web窗體)的形式顯示圖像時,出現問題,以字節[]的形式存儲在db中-找到了很多相當清楚的答案,如何做,所以現在有了一個處理程序:

public class ShowImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //creating object, working with db
        var core = new CoreHolder();
        var picture = core.PictureRepository.Read(Convert.ToInt32(context.Request.QueryString["id"]))
        context.Response.Clear();
        context.Response.ContentType = picture.PictureMimeType;
        //trying to write byte[]
        context.Response.BinaryWrite(picture.PictureData);
        context.Response.End();
    }

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

...以及我的.aspx頁面中的此類字符串:

<asp:Image ID="Image1" runat="server" ImageUrl="~/ShowImageHandler.ashx?id=<%#:Item.ID %>" />
<asp:Image ID="Image1" runat="server" ImageUrl="~/ShowImageHandler.ashx?id=1>" />

問題是:好的,程序以id輸入ProcessRequest,並且在第二個asp圖像字符串的情況下,它會找到帶有數據的圖片,但即使在嘗試BinaryWrite之前,我仍然可以看到context.Response中有實例。 OutputStream:長度,位置-System.NotSupportedExeption,讀/寫超時-System.InvalidOperationExeption。 如果是第一個字符串(用於ListView ItemTemplate中),則OutputStream問題仍然存在+嘗試從查詢字符串獲取id時,所有問題都得到解決。

請幫助)

您在調試器中看到的context.Response.OutputStream.LengthPosition的錯誤無關緊要。 您只能寫入該流,因此應該在調試器顯示中看到異常。

您的代碼看起來不錯,所以我的猜測是,如果您查看URL,則id querystring參數的值將不是整數。 當您嘗試將其轉換為整數時,可能會收到FormatException

您應該通過在瀏覽器的地址欄中放置URL“ ShowImageHandler.ashx?id = 1”來測試處理程序,而不要使用<img />標記。 這樣,如果有異常,您可以獲得真實的堆棧跟蹤信息,而不僅僅是看到損壞的圖像。

暫無
暫無

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

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