perlynsparks
perlynsparks

Reputation: 401

How to pass image to Eval method from handler.ashx to imageurl?

I want to retrieve image from database and display in an aspx page. I use Linq to SQL. And a Generic handler.

Handler2.ashx code:

public void ProcessRequest(HttpContext context)
{
    if (context.Request.QueryString["id"] != null)
    {
        int id;
        string sid = context.Request.QueryString["id"];
        if (int.TryParse(sid, out id))
        {
            Stream strm = getImage(id);
            byte[] buffer = new byte[4096];
            int i = strm.Read(buffer, 0, 4096);
            while (i > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, 4096);
                i = strm.Read(buffer, 0, 4096);
            }
        }
        else
        {
            //
        }

    }
}


public Stream getImage(int id)
{
    using (DummyDBEntities cntx = new DummyDBEntities())
    {
        var db = from c in cntx.Images
                    where c.imageId == id
                    select c.imageData;
        MemoryStream ms = new MemoryStream();
        byte[] byteArray = new byte[db.ToArray().Length];
        ms.Write(byteArray, 0, db.ToArray().Length);

        return new MemoryStream(byteArray);

    }
}

And a button control in Default.aspx page when I click, redirects to handler1.ashx. Gets the id of image from database and supposed to Show it in Default.aspx asp:image control

protected void btnGetID_Click(object sender, EventArgs e)
{
  int id=Convert.ToInt32(txtGetID.Text);
  Response.Redirect("Handler2.ashx?id="+id);
  Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>';
}

How do i supposed to write Eval method and the querystring to pass the image to imageurl?

Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>';

Please help, thanks.

Upvotes: 0

Views: 1312

Answers (1)

Kiran Hegde
Kiran Hegde

Reputation: 3681

I hope I understood your problem right. You want to display the image from your handler in the Image1. For that you can just do the following

Image1.ImageUrl = ResolveUrl("~/Handler2.ashx?id=" + id);

Upvotes: 0

Related Questions