0

我正在开发一个简单的图像标记和搜索应用程序。我已经将我的图像上传到数据库,应用了标签,但是当我将它们拉回来时失败了 - 图像没有呈现。

我在 SO 上找到了这个,但我无法让它工作。

我想我可能误解了处理程序。

简而言之,在后面的代码中,我创建了一个 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
4

3 回答 3

1

看起来您没有在 web.config 中注册处理程序和/或在 IIS 中注册扩展。有关更多详细信息,请参见此处此处

编辑:我现在看到您使用 .ashx 作为扩展名,因此您通常不需要注册它。现在主要线索是 web.config 中的处理程序注册。

于 2009-08-29T13:17:05.873 回答
1

C# 示例,但这对我来说很好 - 您可能想要添加内容长度标头:

<%@ WebHandler Language="C#" Class="Photo" %>

using System;
using System.Web;

public class Photo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(System.IO.File.ReadAllBytes("C:\\Test.jpg"));
        context.Response.AddHeader("Content-Length", new System.IO.FileInfo("C:\\Test.jpg").Length.ToString());
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

基本上,先做一个简单的测试,如果可行,那么我建议这是您从数据库返回的数据。

于 2009-08-30T13:26:44.140 回答
0

好奇:您是否使用 BLOB 来避免热链接?(有更好的方法可以做到这一点)

于 2009-08-29T14:06:22.023 回答