图像是通过一些用户输入参数动态生成的。我能够通过以下方式成功生成图像GET,通过写信给Response.OutputStream
$('#myImage').attr('src', 'Images/GetImage?param1=value1¶m2=value2');
还有几个附加参数。但是,我怎样才能做到这一点POST?我想我可以用$.ajax
和base64编码Image
,但效果不太好。
$.ajax({
url: 'Images/GetImage64',
type: 'POST',
data: { param1: 'value1', param2: 'value2' },
success: function (data) {
//$('#myImage').attr('src', data);
$('#myImage').attr('src', 'data:image/png;base64, ' + data);
}
});
Chrome 开发工具显示 ajax (XHR) POST 到 /Images/GetImage64 并带有文本/纯文本响应。内容看起来像服务器上生成的 PNG。然而,另一个“其他”请求是通过下面的 URL 发出的,我不知道发生了什么
data:image/png:base64, [binary]
在服务器上,我返回一个ImageResult : ActionResult
覆盖ExecuteResult
并使用 Base64 编码图像进行响应。
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = "text/plain";
context.HttpContext.Response.BinaryWrite(GetBase64Image(Image));
}