My开发者_Python百科 controller, in a nutshell is this:
chart1.SeriesCollection.Add(SC);
using (MemoryStream ms = chart1.GetChartStream())
{
return File(ms.ToArray(), "image/png");
}
My view is this:
$('#targetDiv').load("Home/GetImage");
And I'm getting garbled characters when rendered. Any ideas?
thanks, rodchar
You need to use an img
tag:
<img src="Home/GetImage" alt="" />
When you write $('#targetDiv').load("Home/GetImage");
you are basically saying: send a GET requets to Home/GetImage
using Ajax and if the request succeeds update the contents of #targetDiv
with the result. As your controller action sends binary data, this binary data will be injected into the div.
You should set the content type of your response to accommodate the fact you're sending back an image. If you don't your binary stream will be interpreted as text, hence the garbled stuff you get.
There's a related question here: Can an ASP.NET MVC controller return an Image?
Try adding this to your code, before you read the file and send it back
this.Response.Clear();
this.Response.ContentType = "image/png";
on the markup side instead of putting the content into a div you need to put it into an image tag.
精彩评论