views:

31

answers:

3

Hey All,

My 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

+2  A: 

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.

Darin Dimitrov
liggett78
+1  A: 

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: http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image

Yann Schwartz
+1  A: 

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.

Bootcamp