The View looks fine, I just use src="/Controller/GetImage?id=xxxxx" which is effectively the same. The Controller action is a little different in that it's returning a FileContentResult thus. My image in the DB also stores the MIME type, the images are uploaded to the server so I just grab that at upload time.
public FileContentResult GetImage(Guid ImageID)
{
Image image = (from i in myRepository.Images
where i.ImageID == ImageID
select i).SingleOrDefault();
if (image == null)
{
return File(System.IO.File.ReadAllBytes(Server.MapPath("/Content/Images/nophoto.png")), "image/png");
}
else
{
return File(image.ImageBlob, image.ImageMimeType);
}
}
Code to the Image class
[Table(Name="Images")]
public class Image
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public Guid ImageID { get; set; }
[Column]
public bool OnDisk { get; set; }
[Column]
public string ImagePath { get; set; }
[Column]
public byte[] ImageBlob { get; set; }
[Column]
public string ImageMimeType { get; set; }
[Column(AutoSync = AutoSync.Always, DbType = "rowversion NOT NULL", CanBeNull = false, IsDbGenerated = true, IsVersion = true)]
public Binary ConcurrencyStamp { get; set; }
}
Lazarus
2009-09-24 15:41:07