I am trying to display a binary image through an httphandler. Here is the code I have:
ajax call
$.ajax({
type: "GET",
url: "AnswerPostHandler.ashx",
data: "imgid=1380",
success: function (msg) {
$(parent).append(msg);
},
error: function (msg){
alert(msg);
}
});
Handler code
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";
try
{
krystaladbDataContext db = new krystaladbDataContext();
var binimg = (from i in db.image_tables
where i.IMG_ID.Equals(1380)
select i.IMG_THUMB).Single();
byte[] b = binimg.ToArray();
MemoryStream ms = new MemoryStream(b);
Image img = Image.FromStream(ms);
img.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
context.Response.Write(ex);
}
}
something still seems not right here and it is dumping binary data. Base64 encoding is also a choice here but I have tested it , and it doesn’t work on IE 7 ( literally chops part of the image ).
can you advice me on what i am doing wrong here and how I can make it right? thanks
Handler code,
and html
<img>markup,Or you may request via jQuery code: