user3733078
user3733078

Reputation: 249

Can't retrieve binary data

I have binary data in an image column and I am required to retrieve the image and display it. So I have added an ashx file and in the aspx file I have one textbox for image name and one fileupload control. The images are successfully inserted but I think (I am pretty sure) something is wrong in the ashx file but can't find where. I don't get any exceptions. It just doesn't work.

This is my code:

protected void butSubmit_Click(object sender, EventArgs e)
{
    byte[] imgbyte = null;
    if (FileUpload1.HasFile && FileUpload1.PostedFile != null) 
    {
        HttpPostedFile file = FileUpload1.PostedFile;
        imgbyte = new byte[file.ContentLength];
        file.InputStream.Read(imgbyte, 0, file.ContentLength);
    }

    SqlConnection connection = new SqlConnection(@"server=.\sqlexpress;database=projects;Uid=sa;Password=1234");

    connection.Open();
    SqlCommand cmd = new SqlCommand("Insert imgtable values(@title,@image) select @@IDENTITY",connection);
    cmd.Parameters.AddWithValue("@title",txtTitle.Text);
    cmd.Parameters.AddWithValue("@image", imgbyte);
    int id = Convert.ToInt32(cmd.ExecuteScalar());
    Image1.ImageUrl = "~/Handler.ashx?id=" + id;
    connection.Close();
}

This is the generic handler file:

public class Handler : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)
    {
        if(context.Request.QueryString["id"]!=null)
        {
            int id=Convert.ToInt32(context.Request.QueryString["id"]);
            Stream stream = DisplayImage(id);
            context.Response.ContentType = "image/jpeg";
            byte[]img=new byte[stream.Length];
            context.Response.OutputStream.Write(img, 0, img.Length);
        }
    }

    public Stream DisplayImage(int theID)
    {
        SqlConnection con=new SqlConnection (@"server=.\sqlexpress;database=projects;Uid=sa;Password=1234");
        SqlCommand cmd=new SqlCommand ("select image from imgtable where id=@id",con);
        con.Open();
        cmd.Parameters.AddWithValue("@id",theID);
        byte[] img = (byte[])cmd.ExecuteScalar();
        con.Close();
        return new MemoryStream(img);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Upvotes: 0

Views: 131

Answers (1)

Stilgar
Stilgar

Reputation: 23551

You are initializing a new byte array with the length of the stream but there is nothing in that byte array. I am talking about the img byte array in ProcessRequest. Make the DisplayImage method return the byte array and use it directly. Also check with the debugger if img in DisplayImage has data in it.

public class Handler : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)
    {
        if(context.Request.QueryString["id"]!=null)
        {
            int id=Convert.ToInt32(context.Request.QueryString["id"]);
            byte[] img = DisplayImage(id);
            context.Response.ContentType = "image/jpeg";
            context.Response.OutputStream.Write(img, 0, img.Length);
        }
    }

    public byte[] DisplayImage(int theID)
    {
        SqlConnection con=new SqlConnection (@"server=.\sqlexpress;database=projects;Uid=sa;Password=1234");
        SqlCommand cmd=new SqlCommand ("select image from imgtable where id=@id",con);
        con.Open();
        cmd.Parameters.AddWithValue("@id",theID);
        byte[] img = (byte[])cmd.ExecuteScalar();
        con.Close();
        return img;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Upvotes: 1

Related Questions