2009-06-24 79 views
1

生产计算器的图像我有我的居住web应用程序的,我要为给用户的上下文之外图像的目录。目前,我正在使用IHttpHandler来为图像提供服务,并使用一些JavaScript来浏览一组图像(现在导航是原始的)。我遵循使用IHttpHandler为图像提供服务的示例,但是当我在Firefox中查看图像时,浏览器挂起,当我在IE中查看时,出现“堆栈溢出在线:0”。的IHttpHandler在IE

规范了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似乎失控...

回答

2

我的猜测 - 不是JavaScript专家 - 是onload事件在图像加载完成后触发。换句话说,一旦加载图像,它触发装载一个新的...这触发装载一个新的...这触发装载一个新的等

你大概就能看出,在多次调用服务器的同一图像时 - 除非浏览器正在缓存它,当然。无论如何,您需要以其他方式触发它,或者使触发器检测到已加载的图像已经是正确的,并且不需要替换它。

+0

谢谢你,谢谢你。我把onload事件移动到了页面主体(这对我来说很好),并且FF工作正常,堆栈溢出在IE中消失了。 – toddk 2009-06-24 14:22:53

0

我怀疑改变src和加载一个新的形象可能会再次触发图像的“的onload”事件的行为。

尝试设置源之前清除事件,可能会类似于此:

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