2010-07-25 72 views
2

我創建了使用ASHX處理程序從文件系統檢索圖像的代碼。該代碼正確顯示圖像在Chrome,但我在IE中得到一個破碎的形象:ASHX圖像處理程序使用chrome而不是IE8

public class ImageHandler : IHttpHandler 
{ 

    private int? QS_ImageID 
    { 
     get 
     { 
      int? temp = null; 

      if (HttpContext.Current.Request.QueryString["ImageID"] != null) 
       temp = HA.Utility.DataHelper.ParseDbInt(HttpContext.Current.Request.QueryString["ImageID"]); 

      return temp; 
     } 

    } 

    private bool QS_Thumbnail 
    { 
     get 
     { 
      bool thumbNail = false; 
      if (HttpContext.Current.Request.QueryString["Thumbnail"] != null) 
      { 
       if (HttpContext.Current.Request.QueryString["Thumbnail"].Equals("1")) 
        thumbNail = true; 
      } 

      return thumbNail; 
     } 

    } 

    public void ProcessRequest(HttpContext context) 
    { 
     if (QS_ImageID.HasValue) 
     { 
      int uploadID = QS_ImageID.Value; 
      context.Response.ClearHeaders(); 
      context.Response.ClearContent(); 
      context.Response.Cache.SetNoStore(); 
      context.Response.ContentType = UploadDAL.GetMetaData(uploadID).UploadContentType; 
      context.Response.AddHeader("Content-Disposition", "inline;"); 

      if (QS_Thumbnail) 
      { 

       byte[] myImage = UploadDAL.GetFileThumbNail(uploadID); 
       context.Response.AddHeader("Content-Length", myImage.GetLength(0).ToString()); 
       context.Response.BinaryWrite(myImage); 
       context.Response.End(); 
      } 
      else 
      { 
       byte[] myImage = UploadDAL.GetFile(uploadID); 
       context.Response.AddHeader("Content-Length", myImage.GetLength(0).ToString()); 
       context.Response.BinaryWrite(myImage); 
       context.Response.End(); 
      } 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 
+1

您是否考慮過使用[庫來處理縮略圖生成](http://imageresizing.net)?如果你正在做自己的處理程序,[如果沒有,請檢查列表](http://nathanaeljones.com/163/20-image-resizing-pitfalls/)以避免。 – 2011-06-22 23:38:31

+0

我暫時沒有積極開發該代碼,但我認爲使用庫(如您所建議的)是一種可行的方式。謝謝! – Kevin 2011-06-24 15:15:38

回答

3

的問題可能是因爲你沒有指定該文件的MIME類型。下面是一些常見的圖像MIME類型,從函數想回文件的相應MIME類型:

 
string getContentType(String path) 
{ 
    switch (Path.GetExtension(path)) 
    { 
     case ".bmp": return "Image/bmp"; 
     case ".gif": return "Image/gif"; 
     case ".jpg": return "Image/jpeg"; 
     case ".png": return "Image/png"; 
     default : break; 
    } 
    return ""; 
} 

如果圖像的物理存儲在硬盤上,你可以使用這段代碼,因爲它是,如果不是,至少它可以給你一個關於如何確定MIME類型的想法。 在ProcessRequest方法,那麼您需要使用

 
context.Response.ContentType = getContentType(imageFileName); 

當然,正如我之前所說,如果你(如果它存儲在數據庫爲例)沒有該文件的物理路徑,你還應該知道圖像類型。

希望這有助於
安德烈

1

IE確實需要一個MIME類型。

下面我用來獲取MIME類型的圖像,或強制它成爲一個常規文件,並讓系統處理它。

objMIMEType = Microsoft.Win32.Registry.GetValue("HKEY_CLASSES_ROOT\\." + objAttachment.Extension, "Content Type", "application/octetstream"); 

     if (objMIMEType != null) 
      strMIMEType = objMIMEType.ToString(); 
     else 
     { 
      strMIMEType = "application/octetstream"; 
      context.Response.AddHeader("Content-Disposition", "attachment; filename=" + objAttachment.FullFileName); 
      context.Response.AddHeader("Content-Length", objAttachment.AttachmentData.Length.ToString()); 
     }