试图通过HttpHandler显示二进制图像
本文关键字:显示 二进制 图像 HttpHandler | 更新日期: 2023-09-27 18:18:40
我试图通过httphandler显示二进制图像。下面是我的代码:
ajax调用
$.ajax({
type: "GET",
url: "AnswerPostHandler.ashx",
data: "imgid=1380",
success: function (msg) {
$(parent).append(msg);
},
error: function (msg){
alert(msg);
}
});
处理程序代码
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);
}
}
这里似乎有些不对劲,它正在转储二进制数据。Base64编码也是这里的一个选择,但我已经测试过了,它在IE 7上不起作用(字面上切掉了部分图像)。
你能告诉我我在这里做错了什么以及我如何才能改正吗?由于
处理器代码,
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();
context.Response.BinaryWrite(b);
context.Response.Flush();
}
catch (Exception ex)
{
context.Response.Write(ex);
}
}
和html <img>
标记,
<img src="AnswerPostHandler.ashx?imgid=1380" alt="None" id="img1" />
或者你可以通过jQuery代码请求:
$("#showImageButton").click(function () {
$("#img1").attr("src", "AnswerPostHandler.ashx?imgid=1380");
});
这样做会更容易(父). html(美元)
二进制流很难在浏览器中作为img源实现。