0

I have the following controller method which gets a PNG from a web api.

        public async Task<ActionResult> RealTimeUpdate(string fundName)
        {
        string docPath = ConfigurationManager.AppSettings["RealTimeUpdate"].Replace("{fundname}",fundName).ToString();
        docPath = docPath.Replace("\\\\", "\\");
        docPath = docPath.Replace("\"", "");

        string url = ServiceUrl + "api/RealTime/" + fundName;
        HttpResponseMessage response = await client.GetAsync(url);
        if (response.IsSuccessStatusCode)
        {
            var dataStream = response.Content.ReadAsStringAsync().Result;
            if (dataStream == null)
                return HttpNotFound();
            var _buffer = JsonConvert.DeserializeAnonymousType(dataStream, new { _buffer = (byte[])null })._buffer;

            // If user decides to save the file, this will help...
            //Response.AddHeader("content-disposition", "filename=" + Path.GetFileName(path));
            return File(_buffer, "application/png");
        }
        return View("Error");
    }

I call it like this:

<a href="@Url.Action("RealTimeUpdate", "Documents", new { FundName = "RealTimeUpdate"})" class="btn rt-info rtBtn" target="_blank">Real Time Update</a>

As you can see, I have target="_blank", however, instead of displaying the image in a new tab, it downloads it to my documents folder. How can I get it to display in a tab?

Scott
  • 2,456
  • 3
  • 32
  • 54

1 Answers1

0

You need a ImageController to render that.

once you have a controller you can render as follows:

public class ImageController{

public ActionResult ShowImage(string path) 
{

    return File(path);
}

}

in your views:

<img src="@Url.Action("Render","Image", new {id =1  // or path })" />

this answer was taken from https://stackoverflow.com/a/16142574/5586581

Martin Chinome
  • 431
  • 5
  • 17