0

晚安-早-晚-等^_^

我在尝试在 asp:Image (使用 Web 表单)中显示图像时遇到问题,作为 byte[] 存储在数据库中 - 找到了很多相当明确的答案,如何做到这一点,所以现在我有一个处理程序:

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-image 字符串,它会找到带有数据的图片,但即使在尝试 BinaryWrite 之前,我也可以看到,context.Response 中有异常。输出流:长度,位置 - System.NotSupportedExeption,读/写超时 - System.InvalidOperationExeption。如果是第一个字符串(它在 ListView ItemTemplate 中使用),OutputStream 问题仍然存在+所有试图从查询字符串中获取 id 的压力。

请帮忙)

4

1 回答 1

0

context.Response.OutputStream.Length您在调试器中看到的错误Position并不重要。您只能写入该流,因此您在调试器显示中看到的异常是预期的。

您的代码看起来不错,所以我的猜测是,如果您查看 URL,您的id查询字符串参数的值将不是整数。FormatException当您尝试将其转换为整数时,您可能会得到一个。

您应该通过将 url “ShowImageHandler.ashx?id=1” 放在浏览器的地址栏中而不是使用<img />标记来测试处理程序。这样,如果出现异常,您可以获得真正的堆栈跟踪,而不仅仅是看到损坏的图像。

于 2013-11-11T21:20:59.503 回答