2013-06-03 139 views
0

我想在我的視圖中顯示存儲在我的數據庫中的圖像。
我在我的mysql數據庫中有一個類型爲「longblob」的字段「deliverrable_image」。顯示存儲在mysql數據庫中的圖像+ ASP.NET MVC

在我的控制器:

public ActionResult Show(int id) 
    { 
     var imageData = repository.GetAllImages(); 

     return File(imageData, "image/jpg"); 
    } 

庫:

public IQueryable<byte[]> GetAllImages() 
    { 
     return from deliverable in entities.deliverables 
       orderby deliverable.deliverable_image 
       select deliverable.deliverable_image; 
    } 

查看:

<img src='<%= Url.Action("Show", "Deliverable", new { id = ViewData["DeliverableID"] }) %>' /> 

但在我控制我的錯誤:

cannot convert from 'System.Linq.IQueryable' to 'string'

有誰知道我可以如何解決這個問題?

回答

1

在你的操作方法,在第一行得到的圖像集合(具體地說,IQueryable<byte[]>

var imageData = repository.GetAllImages(); 

然後試圖發送所有的圖像作爲單個文件:

return File(imageData, "image/jpg"); 

imageData是一個文件集合,你需要選擇其中的一個並返回它,爲此,你可能需要修改你的GetAllImages函數或者使用不同的函數,這個動作方法被傳遞給id,但你不能在那個函數的結果上使用它,因爲你所有的都是字節數組。您需要過濾指定的id,然後使用生成的字節數組。

+0

你知道我可以在我的資料庫中做到這一點嗎? – nielsv

+0

@ niels123:您可以將'id'傳遞給存儲庫函數,並在LINQ語句中添加where子句,或者從存儲庫中返回更多的字節數組(基本上),並使用' .Where()調用存儲庫函數時。我個人更喜歡後者,因爲它對我來說是有意義的,一個存儲庫是一個獲取和持久化對象的地方,而不僅僅是數據片段。 – David

相關問題