0

I have an ASP.NET MVC app. In this app, I have a Controller that looks like this:

public class MyController 
{
  public ActionResult Index() 
  {
    return View();
  }

  public ActionResult Photos(int id)
  {
    bool usePureImage = false;
    if (String.IsNullOrEmpty(Request.QueryString["pure"]) == false)
    {
      Boolean.TryParse(Request.QueryString["pure"], out usePureImage);
    }

    if (usePureImage)
    {
      // How do I return raw image/file data here?
    }
    else
    {
      ViewBag.PictureUrl = "app/photos/" + id + ".png";
      return View("Picture");
    }
  }
}

I am currently able to successfully hit the Photos route like I want. However, if the request includes "?pure=true" at the end, I want to return the pure data. This way another developer can include the photo in their page. My question is, how do I do this?

JQuery Mobile
  • 6,071
  • 22
  • 74
  • 132

2 Answers2

1

You can return the image as simply a file. Something like this:

var photosDirectory = Server.MapPath("app/photos/");
var photoPath = Path.Combine(photosDirectory, id + ".png");
return File(photoPath, "image/png");

Essentially the File() method returns a raw file as the result.

David
  • 188,958
  • 33
  • 188
  • 262
0

This SO answer appears to have what you need. It uses the File method on the controller to return a FileContentResult that has the file contents.

Community
  • 1
  • 1
Martin Noreke
  • 3,786
  • 19
  • 34