1

have the following image, from a database...

<img src="@Url.Action("GetLogo", "Logo", new { ID = 16})" />

And the controller....

public FileContentResult GetLogo(int ID)
{
    var GetImage = (from x in repository.GetClientLogo 
                    where x.ClientID == ClientID
                    select x).FirstOrDefault();

    ClientLogo clientLogo = GetImage;
    return File(clientLogo.ImageData, clientLogo.ImageMimeType);
}

This all works fine until there is no database record - if this is the case, then I want to return a default image (example <img src="../Images/NoLogo.jpg" />), but I'm having difficulties as the above controller returns FileContentResult.

Any help would be appreciated.

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
Sherry8212
  • 67
  • 1
  • 11

1 Answers1

1
public FileContentResult GetLogo(int ID)
{
    var GetImage = (from x in repository.GetClientLogo
                    where x.ClientID == ClientID
                    select x).FirstOrDefault();
    if (GetImage == null)
    {
      return File(Server.MapPath("/Images/NoLogo.jpg"), "image/jpeg");
    }

    ClientLogo clientLogo = GetImage;
    return File(clientLogo.ImageData, clientLogo.ImageMimeType);
}

Just return NoLogo file when you can't find image for client

Dmytro Rudenko
  • 2,524
  • 2
  • 13
  • 22
  • Sorry, it's now throwing the error: Cannot convert System.Web.Mvc.FilePathResult to System.Web.Mvc.FileContentResult – Sherry8212 Nov 27 '13 at 20:52
  • Got it - changed the return type from FileContentResult to ActionResult, then did.... return (ActionResult)File("../Content/Images/General/bigtick.jpg", "image/jpg"); } else { ClientLogo clientLogo = GetImage; return (ActionResult)File(clientLogo.ImageData, clientLogo.ImageMimeType); – Sherry8212 Nov 27 '13 at 21:49
  • YOu don't need even convert return type to ActionResult, because FileContetntResult and FilePathResult both are descendants of ActionResult – Dmytro Rudenko Nov 28 '13 at 07:10