2011-06-01 140 views
0

我想從數據庫顯示的圖像問題在瀏覽器中呈現的圖像FileContentResult

我在視圖中的動作

public FileContentResult GetImage(string param) 

     { 

     type obj = _someRepository.GetType(param); 

      if (obj!= null && obj.Image!= null) 
      { 

       return File(obj.Image.ToArray(), obj.Image.MimeType); 
      } 

      return "some default image"; 
     } 

< img src="<%:Url.Action("GetImage","ControllerName",new { param= somevalue })%>" alt="some text" 
      width="100px" height="100px" /> 

我也有

(Html.BeginForm("actionname", "controllername", FormMethod.Post, new { enctype = "multipart/form-data" }) 

集。

的圖像數據是從數據庫中獲取,但我看不到在瀏覽器中的圖像, 是有,我失去了一些東西?

回答

1

這裏是我會爲了找出問題執行的步驟。開始從你的硬盤上的某個地方返回一些硬編碼的圖像一個簡單的控制器操作:

public ActionResult GetImage(string param) 
{ 
    byte[] image = File.ReadAllBytes(@"c:\work\foo.png"); 
    return File(image, "image/png"); 
} 

現在在瀏覽器中直接導航到/ControllerName/GetImage,你應該看到的圖像。

下一步是從數據庫中獲取圖像,並可能將其存儲在您的硬盤,以確保它是一個有效的圖像:

type obj = _someRepository.GetType(param); 
File.WriteAllBytes(@"c:\work\foo.png", obj.Image.ToArray()); 

現在籤生成的文件,看看它是否是有效的。最後一步是確保在<img>標籤生成的URL是一樣的,你直接用於測試之一。然後看FireBug's Net標籤,看看瀏覽器中正確請求圖像的,什麼是服務器的回報。

最有可能出現的問題是字節數​​組返回從數據庫中是無效的圖像或爲空。

至於你在問題中顯示的表單,這是用於上傳文件,它與從控制器動作提供動態圖像無關,所以我沒有看到它可能與你的問題有什麼關係。