2

我创建了代码来使用 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;
        }
    }

}
4

2 回答 2

3

问题可能是您没有为文件指定 MIME 类型。以下是一些常见的图像 mime 类型,来自一个旨在返回文件相应 mime 类型的函数:

字符串 getContentType(字符串路径)
{
    开关(Path.GetExtension(路径))
    {
        案例“.bmp”:返回“图像/bmp”;
        案例“.gif”:返回“图像/gif”;
        案例“.jpg”:返回“图像/jpeg”;
        案例“.png”:返回“图像/png”;
        默认:中断;
    }
    返回 ””;
}

如果图像物理存储在硬盘驱动器上,您可以按原样使用这段代码,至少它可以让您了解如何确定 MIME 类型。在该ProcessRequest方法中,您将使用

context.Response.ContentType = getContentType(imageFileName);

当然,正如我之前所说,如果您没有文件的物理路径(例如,如果它存储在数据库中),您仍然应该知道图像类型。

希望这会有所帮助,
安德烈

于 2010-07-25T05:22:30.090 回答
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());
        }
于 2010-08-18T18:13:34.867 回答