2009-06-24 80 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; 
}