我使用EF 4.1代碼第一次爲簡單起見,假設我有以下的實體類:轉換字節數組圖像並顯示在Razor視圖
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Byte[] Image { get; set; }
}
我已成功創建一個工作的創建視圖,允許添加一個Person對象到數據庫中。
但是,當我來顯示一個人的詳細信息時,我卡住顯示圖像。幾個小時谷歌搜索後,我有以下幾點:
// To convert the Byte Array to the author Image
public FileContentResult getImg(int id)
{
byte[] byteArray = DbContext.Persons.Find(id).Image;
return byteArray != null
? new FileContentResult(byteArray, "image/jpeg")
: null;
}
而且在我試圖名單中的人士細節來看,我有以下的,以獲得要顯示的圖片:
<img src="@Html.Action("getImg", "Person", new { id = item.Id })" alt="Person Image" />
但是上面是無法正常工作,我的圖片來源[src]屬性返回空。
我會非常感謝一些幫助與獲取我的圖像顯示。
謝謝。
Ciwan。
真棒,似乎現在的工作,**謝謝達林** :) – Ciwan
但如果是一些什麼PDF文件的字節數組。我們如何顯示預覽鏈接或可能是下載PDF的鏈接。 。 – Unbreakable
如果它不是圖像會怎麼樣?我們如何處理這個問題。很明顯,我們不能把PDF格式的img src標籤作爲 – Unbreakable