Reputation: 10753
Images are generated dynamically with a handful of user input parameters. I'm able to successfully produce the image via GET, by writing to Response.OutputStream
$('#myImage').attr('src', 'Images/GetImage?param1=value1¶m2=value2');
There are several additional parameters. But, how can I accomplish this with a POST? I thought I might use $.ajax
and base64 encode the Image
, but it doesn't quite work.
$.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 dev tools shows an ajax (XHR) POST to /Images/GetImage64 with a text/plain response. The content looks like the PNG generated on the server. However, another "Other" request is made with the URL below, and I have no idea what's going on
data:image/png:base64, [binary]
On the server, I'm returning an ImageResult : ActionResult
that overrides ExecuteResult
and response with base64 encoded image.
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = "text/plain";
context.HttpContext.Response.BinaryWrite(GetBase64Image(Image));
}
Upvotes: 2
Views: 7280
Reputation: 1713
Might be jQuery's fault. Try forcing jQuery to treat the result as plain text with dataType: 'text'
:
$.ajax({
url: 'Images/GetImage64',
type: 'POST',
dataType: 'text',
data: { param1: 'value1', param2: 'value2' },
success: function (data) {
$('#myImage').attr('src', 'data:image/png;base64,' + data);
}
});
Edit: Nevermind, wasn't that.
Upvotes: 1