使用处理程序从 ASP.NET 页面上的 Blob 呈现图像。我无法得到它
我正在开发一个简单的图像标记和搜索应用程序。 我已经将图像上传到数据库,应用了标签,但是当我将它们拉回时失败 - 图像不渲染。
我在这里找到了这个,但是我'我无法让它工作。
我想我可能误解了处理程序。
简而言之,在后面的代码中,我创建一个 ASP:Image,将其 imageurl 设置为带有照片 id 的处理程序,然后将该控件添加到 ASP:Placeholder。
当页面呈现时,在 IE 中,我得到了那个小红色 x 无图像的东西,而在 FF 中,什么也没有。
让我觉得我错过了什么的一件事是我的处理程序代码中的断点从未被命中。所以它甚至被执行了。正确的?
有人知道我在这里做错了什么吗?谢谢。
这是我的处理程序
Imports aapeClsLib
Imports System.Web
Imports System.Web.Services
Public Class photos
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim img As Byte() = getImage(context.Request.QueryString("ID"))
context.Response.Clear()
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(img)
context.Response.End()
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Private Function getImage(ByVal id As String) As Byte()
Dim img As Byte()
Dim strSql As String = "select ph_photo from photos where ph_id = " & id
Dim dt As DataTable = sqLiteData.getDataTable(strSql)
img = CType(dt.Rows(0)(0), Byte())
Return img
End Function
End Class
以及我将其粘贴在占位符中的位置
Private Sub insertPhotos(ByVal dt As DataTable)
For Each row As DataRow In dt.Rows
Dim img As New Image
img.ImageUrl = "photos.ashx?ID=" & row(0)
PlaceHolder1.Controls.Add(img)
Next
End Sub
I'm working on a simple image tagging and searching app.
I've got my images uploading to the DB, the tags being applied, but am failing when I pull them back - the images don't render.
I found this here on SO, but I'm not able to get it working.
I think I am perhaps misunderstanding handlers.
In short, in the code behind, I'm creating an ASP:Image, setting its imageurl to the handler with the id of the photo, and then adding that control to an ASP:Placeholder.
When the page renders, I get, in IE, that little red x no image thing, and in FF, nothing.
One thing that gets me thinking I'm missing something is that a breakpoint in my handler code is never hit. So it's even getting executed. Right?
Anyone know what I'm doing wrong here? Thanks.
Here's my handler
Imports aapeClsLib
Imports System.Web
Imports System.Web.Services
Public Class photos
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim img As Byte() = getImage(context.Request.QueryString("ID"))
context.Response.Clear()
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(img)
context.Response.End()
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Private Function getImage(ByVal id As String) As Byte()
Dim img As Byte()
Dim strSql As String = "select ph_photo from photos where ph_id = " & id
Dim dt As DataTable = sqLiteData.getDataTable(strSql)
img = CType(dt.Rows(0)(0), Byte())
Return img
End Function
End Class
and where I'm sticking it in my placeholder
Private Sub insertPhotos(ByVal dt As DataTable)
For Each row As DataRow In dt.Rows
Dim img As New Image
img.ImageUrl = "photos.ashx?ID=" & row(0)
PlaceHolder1.Controls.Add(img)
Next
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 示例,但这对我来说效果很好 - 您可能想要添加内容长度标头:
基本上,只需先做一个简单的测试,如果有效,那么我建议它是您从数据库返回的数据。
C# example, but this works fine for me - you might want to add the content length header:
Basically, just do a simple test first, if that works then I'd suggest it's the data you're returning from the database.
您似乎没有在 web.config 中注册处理程序和/或 IIS 中的扩展。请参阅此处和此处了解更多详细信息。
编辑:我现在看到您正在使用 .ashx 作为扩展名,因此您通常不需要注册它。现在主要线索是 web.config 中的处理程序注册。
It looks like you didn't register the handler in web.config and/or the extension in IIS. See here and here for more details.
EDIT: I see now that you are using .ashx as the extension, so you usually don't need to register it. Now the main clue is the handler registration in web.config.
好奇:您是否使用 BLOB 来避免热链接?
(有很多更好的方法可以做到这一点)
Curious: Are you using BLOB's to avoid hot-linking?
(There are much better ways to do this)