I have an ActionMethod which can return a Memorystream or byte[] of an image. This action method is called on ajax. Now I want to bind this image to an image tag src attribute.
public ActionResult GetBpChart(string Timespan)
{
var ObjMemoryStream = new MemoryStream();
try
{
ObjMemoryStream = MyModel.GetImage(Timespan);
}
catch (Exception Ex)
{
}
//return Content(Convert.ToBase64String(ObjMemoryStream.GetBuffer()));
//return Json(ObjMemoryStream.GetBuffer());
return File(ObjMemoryStream.GetBuffer(), "image/jpeg");
}
function GetChart() {
try {
$.ajax({
type: 'POST',
url: "myActionMethodurl",
data: {
Timespan: $('#ddlBpTimespan').val()
},
success: function (ImgSrc) {
// For Base64String
//$('#divBpChart').innerHTML = '<img src= "data:image/jpeg;base64," ' + ImgSrc + '"/>';
// For Json of byte[]
//$('#divBpChart').innerHTML = '<img src= "data:image/gif;base64," ' + ImgSrc + '"/>';
// For FileContentResult
$('#imgBpChart').attr('src', ImgSrc);
HideAjaxProgress();
}
});
} catch (E) {
}
}
I tried the above 3 combinations. But no luck. Can someone say what I am missing here or what's the correct way to achieve this