2012-07-23 76 views
0

什麼是使用MVC3在Web服務器上處理圖像的一些最佳方法。有沒有這方面的最佳做法?通過處理圖像,我的意思是允許用戶在Web服務器上上傳照片,將它們保存在磁盤上,並在必要時檢索它們以在頁面上顯示。使用MVC3處理圖像

回答

1

將圖像保存到磁盤並將路徑存儲在數據庫中通常是最好和最快的方法。上傳可以通過任何常規方式進行處理,但有一些很好的開源庫可以幫助您(plupload是我用過的最完整的功能)。

您可以創建自己的ActionResult實現並返回並顯示圖像。您的控制器操作可以採取您需要的任何參數來識別圖像,然後您可以從磁盤中檢索並從操作中返回ImageResult

這是一個基本的實現(credit):

public class ImageResult : ActionResult 
{ 
    public ImageResult() { } 
    public Image Image { get; set; } 
    public ImageFormat ImageFormat { get; set; } 
    public override void ExecuteResult(ControllerContext context) 
    { 
     // verify properties 
     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"; 
     Image.Save(context.HttpContext.Response.OutputStream, ImageFormat); 
    } 
}