的IHttpHandler用于产生在一个IE计算器图像
-
10-07-2019 - |
题
我有我的居住web应用程序的,我要为给用户的上下文之外图像的目录。目前我使用的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
下面是如上所定义的JavaScript代码的呼唤了IHttpHandler:
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
事件被触发任何时候图像加载完成。换句话说,一旦图像被加载时,它触发装载一个新...触发装载一个新...触发装载一个新的等
您可能能够看到,在多次调用服务器相同的图像 - 除非浏览器缓存它,当然。无论如何,你要么需要触发它在一些其他的方式,或使触发检测已加载的图片已经是正确的,而且也没有必要更换。
其他提示
我怀疑改变src和加载新的图像可以被再次触发的图像的“的onload”事件的动作。
尝试将源之前清除事件,可能会类似于此:
function retrievePicture(imgCtrl, picid)
{
imgCtrl.onload = null;
imgCtrl.src = 'ShowImage.ashx?id=' + picid;
}
不隶属于 StackOverflow