gopal vinnamala

gopal vinnamala

  • NA
  • 136
  • 9.3k

Error in Retrieving Binary Image using generic Handler

Jan 23 2015 7:29 AM
public class Handler : IHttpHandler
{
static DataTable dt;
string url = ConfigurationManager.AppSettings["ServiceUrl"].ToString();

public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["id"];
if (id != null)
{

MemoryStream memoryStream = new MemoryStream();

string json = class.HttpGet(url + "Services/Product.svc/ProductByRowID/" + id);
json = Regex.Unescape(json);
dt = (DataTable)JsonConvert.DeserializeObject(json.Trim(new Char[] { ' ', '"', '.' }), typeof(DataTable));

//Get Image Data

byte[] file = (byte[])Convert.FromBase64String(dt.Rows[0]["ProductImage"].ToString());
context.Response.ContentType = "image/jpg";
context.Response.OutputStream.Write(file, 78, file.Length - 78);

memoryStream.Write(file, 0, file.Length);
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
memoryStream.Dispose();



}
}




public bool IsReusable
{
get
{
return false;
}
}

}
 
 
 
and in   .aspx I tried to access the image files as
 
<asp:Image ID="Image" runat="server" ImageUrl='<%# "Handler.ashx?id=" + Eval("ProductRowId")%>' />
 

Answers (1)