public class Handler : IHttpHandler
{
static DataTable dt;
string url = ConfigurationManager.AppSettings["ServiceUrl"].ToString();
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["id"]; //get the querystring value that was pass on the ImageURL (see GridView MarkUp in Page1.aspx)
if (id != null)
{
MemoryStream memoryStream = new MemoryStream();
string json = ClsUtility.HttpGet(url + "Services/service.svc/ProductS/" + 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();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
这里我没有收到任何错误而没有显示图片
在.aspx中我尝试访问图像文件
<asp:Image ID="Image" runat="server" ImageUrl='<%# "Handler.ashx?id=" + Eval("ProductRowId")%>' />
不是产品名称告诉我在处理程序中做了什么错误