简体   繁体   English

从db和OutputStream异常显示图像ASP.NET/C#

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

Good night-morning-evening-etc ^_^ 晚安,晚上等等等^ _ ^

I'm getting problems while trying to display an image in asp:Image (using Web Forms), stored in a db as byte[] - found a lot of rather clear answers, how to do it, so now I have a handler: 尝试以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;
        }
    }
}

...and such strings in my .aspx page: ...以及我的.aspx页面中的此类字符串:

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

The problems are: ok, the program enters the ProcessRequest with id, and in case of the second asp-image string it finds the pic with data, but even before trying to BinaryWrite I can see, that there are exeptions in context.Response.OutputStream: length,position - System.NotSupportedExeption, Read/WriteTimeout - System.InvalidOperationExeption. 问题是:好的,程序以id输入ProcessRequest,并且在第二个asp图像字符串的情况下,它会找到带有数据的图片,但即使在尝试BinaryWrite之前,我仍然可以看到context.Response中有实例。 OutputStream:长度,位置-System.NotSupportedExeption,读/写超时-System.InvalidOperationExeption。 In case of the first string (it's used in ListView ItemTemplate) the OutputStream problem stays + all crushes on trying to get the id from the query string. 如果是第一个字符串(用于ListView ItemTemplate中),则OutputStream问题仍然存在+尝试从查询字符串获取id时,所有问题都得到解决。

Help, please) 请帮助)

The errors you see in the debugger for context.Response.OutputStream.Length and Position don't matter. 您在调试器中看到的context.Response.OutputStream.LengthPosition的错误无关紧要。 You can only write to that stream so the exceptions you see in the debugger display are expected. 您只能写入该流,因此应该在调试器显示中看到异常。

Your code looks fine so my guess is that if you look at the URL, the value for your id querystring argument will not be an integer. 您的代码看起来不错,所以我的猜测是,如果您查看URL,则id querystring参数的值将不是整数。 You are probably getting a FormatException when you try to convert it to an integer. 当您尝试将其转换为整数时,可能会收到FormatException

You should test the handler by putting the url "ShowImageHandler.ashx?id=1" in your browser's address bar instead of using an <img /> tag. 您应该通过在浏览器的地址栏中放置URL“ ShowImageHandler.ashx?id = 1”来测试处理程序,而不要使用<img />标记。 That way if there is an exception you can get a real stack trace instead of just seeing a broken image. 这样,如果有异常,您可以获得真实的堆栈跟踪信息,而不仅仅是看到损坏的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM