selva
selva

Reputation: 809

Do we need to create an HttpHandler(ashx) for each image?

In my page i'm trying to show two images which have the same id. For that, I have two image controls(imgX,imgY). To write the image to the image control I am using an HttpHandler(ashx).

My problem is that i'm getting the same image appending to both controls(imgX,imgY)

In page load event this is my code:

imgPhoto.ImageUrl = "Image.ashx?EmpBadge=" & Session("EmpBadge")
imgSign.ImageUrl = "Image.ashx?EmpBadge=" & Session("EmpBadge")

And in ashx:

  Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim imageId As String = context.Request.QueryString("EmpBadge")
            Dim drPhoto As SqlDataReader
            Dim param(1) As SqlParameter
            param(1) = New SqlParameter("@EmpBadge", imageId)
            drPhoto = IMSIndia.ExecuteReaderWithParam("SpGetPhotoAndSignature", param)
            If drPhoto.HasRows Then
                While drPhoto.Read()
                    context.Response.ContentType = "image/" & drPhoto("PhotoType")
                    context.Response.BinaryWrite(DirectCast(drPhoto("Photo"), Byte()))
                    context.Response.ContentType = "image/" & drPhoto("Signaturetype")
                    context.Response.BinaryWrite(DirectCast(drPhoto("Signature"), Byte()))
                End While
            End If
        Catch ex As Exception

        Finally
            If IMSIndia.con.State = ConnectionState.Open Then
                IMSIndia.ConnectionClose()
            End If
        End Try
    End Sub

Thanks.

Upvotes: 0

Views: 440

Answers (2)

codeandcloud
codeandcloud

Reputation: 55210

You should be altering your code like this.

At Page_Load

imgPhoto.ImageUrl = "Image.ashx?ImageType=photo&EmpBadge=" & Session("EmpBadge")
imgSign.ImageUrl = "Image.ashx?ImageType=signature&EmpBadge=" & Session("EmpBadge")

Inside the while loop inside the ProcessRequest, place an if-else like this.

If drPhoto.HasRows Then
    While drPhoto.Read()
        If context.Request.QueryString("ImageType") = "photo" Then
            context.Response.ContentType = "image/" & drPhoto("PhotoType")
            context.Response.BinaryWrite(DirectCast(drPhoto("Photo"), Byte()))
        Else
            context.Response.ContentType = "image/" & drPhoto("Signaturetype")
            context.Response.BinaryWrite(DirectCast(drPhoto("Signature"), Byte()))
        End If
    End While
End If

Upvotes: 1

Andrew Barber
Andrew Barber

Reputation: 40150

Umm... of course you are getting the same image for each; you are passing exactly the same URL for each. You are using the same Session value for both.

Then, in your code you seem to be trying to send two images within the same response. That makes no sense at all, to the point that I'm not sure if it's related to this problem in the first place.

You need to differ the image based on the QueryString value. Your handler can't tell the difference unless you do.

Upvotes: 1

Related Questions