开发者

chart stream not rendering correctly

开发者 https://www.devze.com 2022-12-11 00:29 出处:网络
My开发者_Python百科 controller, in a nutshell is this: chart1.SeriesCollection.Add(SC); using (MemoryStream ms = chart1.GetChartStream())

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消