C# ASHX图像处理程序使用chrome,而不是IE8

C# ASHX图像处理程序使用chrome,而不是IE8,c#,asp.net,ashx,C#,Asp.net,Ashx,我创建了使用ASHX处理程序从文件系统检索图像的代码。代码在Chrome中正确显示图像,但我在IE中得到一个损坏的图像: public class ImageHandler : IHttpHandler { private int? QS_ImageID { get { int? temp = null; if (HttpContext.Current.Request.QueryString["I

我创建了使用ASHX处理程序从文件系统检索图像的代码。代码在Chrome中正确显示图像,但我在IE中得到一个损坏的图像:

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;
        }
    }

}

问题可能是您没有为文件指定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 ""; } 当然,正如我之前所说的,如果您没有文件的物理路径(例如,如果它存储在数据库中),那么您仍然应该知道图像类型

希望这有帮助,

Andrei

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());
        }

你有没有考虑过使用一种新的方法?如果你在做你自己的处理程序,为了避免。我已经有一段时间没有积极开发代码了,但我认为使用库(正如你所建议的)是一种方法。谢谢
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());
        }