3

Handler is not returning image. If I remove conditional statement, handler is returning image. This is my code

public void ProcessRequest(HttpContext context)
    {
        string sid = "JUN15MBACHN001";
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        connection.Open();
        SqlCommand command = new SqlCommand("select suppassportphoto from studdetails where sregno=" + sid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        Byte[] br = (Byte[])dr[0];
        if (br.Length > 1)
        {
            context.Response.BinaryWrite((Byte[])dr[0]);
        }          
        else
        {
            string path = context.Server.MapPath("~/image/emptymalepic.jpg");
            byte[] byteArray = File.ReadAllBytes(path);
            context.Response.BinaryWrite(byteArray);
        }
        connection.Close();
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

I don't know where I'm going wrong ? Any help would be appreciated.

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54

3 Answers3

1

First of all i'd recommend to cast in a different way because your code might cause a invalid cast exception:

Byte[] br = dr[0] as Byte[];

Then check for null

if (br != null && br.Length > 1)

Then write the file:

context.Response.ContentType = "image/jpeg"; // or whatever type you're using
context.Response.BinaryWrite(br);

And replace

context.Response.End();

with

HttpContext.Current.ApplicationInstance.CompleteRequest();

Because i've noticed that somehow some browsers don't like the Response.End()

Hope this is usefull. Good luck!

Clark Kent
  • 305
  • 1
  • 4
  • 16
0

As I can notice, In your code br.Length is always less then 1

Try this code snippet link

{
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");

byte[] MyData= new byte[0];

da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];

MyData =  (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0); 

FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
}
slava
  • 1,901
  • 6
  • 28
  • 32
0

try this

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim _ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString")
        Dim UserId As String = context.Request.QueryString("Id")
        Dim con As New SqlConnection(_ConnectionString)
        Try
            con.Open()
            Dim cmd As New SqlCommand(Convert.ToString("select UserPhoto from tblEmployee where EmpId=") & UserId, con)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            dr.Read()
            If Not dr.IsDBNull(0) Then
                context.Response.BinaryWrite(DirectCast(dr(0), Byte()))
            Else
                Dim imgpath As String = context.Server.MapPath("~/images/images.jpg")
                Dim byteArray As Byte() = File.ReadAllBytes(imgpath)
                context.Response.BinaryWrite(byteArray)
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub
Annalaxmi
  • 135
  • 1
  • 8