The following code, we hope, is the correct way to return an image that exists on disk using ASP.NET MVC 3:
public FilePathResult GetThumbnail(string imageName)
{
if( !String.IsNullOrEmpty(imageName) &&
Regex.IsMatch(imageName, @"^p\d{10}.jpg$"))) )
{
var homePath = Server.MapPath("~/Content/previews");
var imagePath = Path.Combine( homePath, imageName );
if( System.IO.File.Exists(imagePath) )
return this.File(imagePath, "image/jpeg");
}
return ???
}
If you didn’t find the file so that you could return, it will represent HTML 404 error (or equivalent?)
source
share