2009-11-18 100 views
3

我有一個包含用戶ID和圖像列的視圖。在asp.net中顯示數據庫中的圖像mvc

下面是我試圖做的檢索圖像,但我不斷收到一個紅色的x,而不是實際的圖像。

查看

<td><img src="<%= Url.Action("DisplayImage" , "User" , new { id = item.id}) %>" alt="" /></td> 

控制器

public FileContentResult DisplayImage(string id) 
    { 
     byte[] image = repository.GetImage(id); 
     return File(image, "image/jpg"); 
    } 

我也試過,而不是返回一個ActionResult,並且也不能工作。

public Byte[] GetImage(string id) 
    { 

     var image = db.GetImage(id).First<GetImageResult>(); 

     if (image == null) 
      return null; 
     return image.UserImage; 
    } 

LinqTOSQL類

[Function(Name="dbo.GetImage")] 
public ISingleResult<GetImageResult> GetImage([Parameter(DbType="VarChar(8)")] string id) 
{ 
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id); 
    return ((ISingleResult<GetImageResult>)(result.ReturnValue)); 
} 

public partial class GetImageResult 
{ 
    private System.Byte[] _userImage; 

    public GetImageResult() 
    { 
    } 


    [Column(Storage="_userImage", DbType="Image")] 
    public System.Byte[] UserImage 
    { 
     get 
     { 
      return this._userImage; 
     } 
     set 
     { 
      if ((this. _userImage!= value)) 
      { 
       this. _userImage = value; 
      } 
     } 
    } 
} 

我已經殺死自己整天都試圖得到這個工作,但它只是不工作。 存儲過程的返回類型是一個整數(至少當我查看參數 SQL Server Management Studio中的整數)時,但我無法重新定義現在可以嗎?

它實際上是用UserController中正確的參數觸發DisplayImage Action並返回File(imageByteArray,「image/jpg」),但只顯示一個帶紅色x的框。 任何幫助將不勝感激。

編輯:我已經嘗試通過在操作結果中添加一個Reponse.BinaryWrite(imageByteArray)並直接擊中url來轉到http://localhost/User/DisplayImage?id=10101010,並且該用戶的圖像顯示在mspaint中。

edit2:我也做了一個視圖源,我的HTML圖像標籤出來如下。

<td> 
    <img src='/User.mvc/GetImage?id=U00915441' alt="" /> 
</td> 

感謝

+0

我有這樣的問題,它只是試圖找到圖像的路徑,而不是從路線。您是否嘗試過調試以查看應用程序是否進入控制器? – 2009-11-18 17:49:41

+0

它確實擊中了控制器動作DisplayImage(字符串ID),並且它沒有任何問題地通過它 – zSynopsis 2009-11-18 18:29:15

+0

也使用fiddler來查看到底是什麼回來 – 2009-11-18 19:09:32

回答

3

看看這個問題,我從一陣回來了 - 該解決方案爲special ActionResult type for images

編輯:這是我的代碼。我實際上是從我使用GDI +創建的Image創建ImageResult類,如下所示:

return new ImageResult() 
{ 
     ImageFormat = spriteInfo.ImageFormat, 
     EncodedImageBytes = spriteInfo.GetImageStream() 
}; 

圖像結果類是。你會注意到,如果我提供EncodedImageBytes參數,它會將它發送到輸出流。這看起來正是你想要的。另一方面,如果你只是傳入一個圖像,那麼它只會將該圖像寫入輸出流。

public class ImageResult : ActionResult 
    { 
     public ImageResult() { } 
     public int? Quality { get; set; } 
     public Image Image { get; set; } 
     public ImageFormat ImageFormat { get; set; } 
     public byte[] EncodedImageBytes { get; set; } 

     public override void ExecuteResult(ControllerContext context) 
     { 
      // verify properties 
      if (EncodedImageBytes == null) 
      { 
       if (Image == null) 
       { 
        throw new ArgumentNullException("Image"); 
       } 
      } 
      if (ImageFormat == null) 
      { 
       throw new ArgumentNullException("ImageFormat"); 
      } 
      // output 
      context.HttpContext.Response.Clear(); 

      if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp"; 
      if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif"; 
      if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon"; 
      if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg"; 
      if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png"; 
      if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff"; 
      if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf"; 

      // output stream 
      Stream outputStream = context.HttpContext.Response.OutputStream; 
      if (EncodedImageBytes != null) 
      { 
       outputStream.Write(EncodedImageBytes, 0, EncodedImageBytes.Length); 
      } 
      else 
      { 
       ImageUtil.SaveImageToStream(outputStream, Image, ImageFormat, Quality); 
      } 
     } 

    } 
+0

回覆回來,如果這並不幫助和虐待使用。我其實創建圖像動態地挖開代碼IM併爲它們提供沒有問題 - 所以我從字節流 – 2009-11-18 19:06:17

+0

謝謝。你能請張貼一些代碼嗎? – zSynopsis 2009-11-18 19:46:07

+0

希望這可以幫助 – 2009-11-18 20:10:52