asp.net - IHttpHandler 用于在 IE 中生成 stackoverflow 的图像

标签 asp.net javascript vb.net ihttphandler

我有一个图像目录,该目录位于我需要提供给用户的网络应用程序上下文之外。目前我正在使用 IHttpHandler 来提供图像并使用一些 javascript 在一组图像中导航(目前导航是原始的)。我遵循使用 IHttpHandler 密切提供图像的示例,但是当我在 firefox 中查看图像时,浏览器挂起,而当我在 IE 中查看时,我得到“第 0 行的堆栈溢出”。

IHttpHandler 的代码

Public Class ShowImage : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) _
                               Implements IHttpHandler.ProcessRequest
        Dim picid As String
        If context.Request.QueryString("id") IsNot Nothing Then
            picid = context.Request.QueryString("id")
        Else
            Throw New ArgumentException("No parameter specified")
        End If

        '' Convert Byte[] to Bitmap
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        context.Response.Cache.SetNoStore()
        context.Response.Cache.SetExpires(DateTime.MinValue)

        Dim newBmp As Bitmap = GetPhoto(picid)
        If newBmp IsNot Nothing Then
            Dim imgGraphics As Graphics = Graphics.FromImage(newBmp)
            imgGraphics.DrawImageUnscaled(newBmp, 0, 0, 640, 480)

            context.Response.StatusCode = 200
            context.Response.ContentType = "image/jpeg"
            newBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg)
            newBmp.Dispose()
        Else
            '' Return 404
            context.Response.StatusCode = 404
            context.Response.End()
        End If

    End Sub

    ...

    Public ReadOnly Property IsReusable() As Boolean _
                        Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property
End Class

这是调用上面定义的 IHttpHandler 的 javascript 代码:

function updateImage(){
    var ddlPhotos = document.getElementById("ddlPhotos");
    var selected = ddlPhotos.options[ddlPhotos.selectedIndex].value;
    if( selected != -1 ){
        // Update the image
        retrievePicture(document.getElementById("propertyImage"), selected)
    }
}

function retrievePicture(imgCtrl, picid)
{
    imgCtrl.src = 'ShowImage.ashx?id=' + picid;
}

最后是作为“占位符”的 img 标签:

<img src="#" 
     alt="Property Photo" 
     width="640px" 
     height="480px" 
     id="propertyImage" 
     onload="retrievePicture(this, '<%= pictureId.value  %>');"
/>

我很困惑为什么 javascript 似乎失控了......

最佳答案

我的猜测(不是 JavaScript 专家)是 onload 事件会在图像加载完成时触发。换句话说,一旦图像被加载,它就会触发加载一个新图像...触发加载一个新图像...触发加载一个新图像等。

您可能会在针对同一图像对服务器的多次调用中看到这一点 - 当然,除非浏览器正在缓存它。无论如何,您要么需要以其他方式触发它,要么让触发器检测到已加载的图像已经是正确的图像,并且无需替换它。

关于asp.net - IHttpHandler 用于在 IE 中生成 stackoverflow 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1038655/

相关文章:

vb.net - 从十进制中删除尾随零的最佳方法

vb.net - 覆盖 datagridview 上的 keydown Enter

.net - 强制提示IE下载框

javascript - 使用 jQuery 的each() 打开下载窗口

c# - 反 xsrf token 验证失败

javascript - h1 在 Canvas 上但不可选择/不可触摸?

javascript - 变量访问的 Angular 和 javascript 范围

asp.net - 隐藏 gridview 列标题

c# - ApplicationException的使用

c# - 如何将具有值的新列添加到现有数据表?