0

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

Rod
  • 14,529
  • 31
  • 118
  • 230

3 Answers3

2

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • +1. The proper way to do it with Ajax would be to set the "src"-attribute of the img-tag to the new value (e.g. getimage?value1=&value2 etc.) – liggett78 Nov 04 '09 at 13:36
1

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?

Community
  • 1
  • 1
Yann Schwartz
  • 5,954
  • 24
  • 27
1

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.

Ravinder Singh
  • 846
  • 1
  • 7
  • 17