views:

341

answers:

3

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
+1  A: 

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.

Fabrizio C.
So for IIS7 in Integrated Mode, I need to add the following to my web.config? <system.webServer> <handlers> <add name="photos" verb="*" path="*.ashx" type="photos" /> resourceType="Unspecified" /> <handlers> <system.webServer>
aape
Sorry about the formatting.
aape
Just added that and after I fixed my missing closing tags, it still doesn't load.I did it on the live site, as I'm at home and don't have a development box here to mess with.
aape
You have also to add the entry in the system.web section.
Fabrizio C.
A: 

Curious: Are you using BLOB's to avoid hot-linking? (There are much better ways to do this)

msftwise
No, but that answers another question that's been bouncing around in my head. I'm just doing it to do it. I've finally got [a domain](http://www.asocialindustries.com), and can use it to just try out things and learn new stuff - at work, I'm always doing their things, and it's all intranet anyway, so I'll never be able to use it for a portfolio or anything.
aape
+1  A: 

C# example, but this works fine for me - you might want to add the content length header:

<%@ 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;
        }
    }

}

Basically, just do a simple test first, if that works then I'd suggest it's the data you're returning from the database.

Richard Green
Ga! I'm a total nubcaeks (sorry, the phrase cracks me up). I was writing nothing to the DB - in my upload, I was just setting the size of the byte() and uploading it empty. Thanks man!
aape