c# - ASHX 图像处理程序适用于 Chrome,不适用于 IE8

标签 c# asp.net ashx

我已经创建了使用 ASSX 处理程序从文件系统检索图像的代码。该代码在 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 "";
}

如果图像物理存储在硬盘上,您可以按原样使用这段代码,如果不是的话,至少它可以让您了解如何确定 mime 类型。 在 ProcessRequest 方法中,您可以使用

context.Response.ContentType = getContentType(imageFileName);

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

希望这有帮助,
安德烈

关于c# - ASHX 图像处理程序适用于 Chrome,不适用于 IE8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3327605/

相关文章:

c# - 查找特定格式的字符串在给定文本中出现的次数

c# - 如何将字符串数组转为int数组进行计算?

c# - Dictionary 和 BackGroundWorker,收到 InvalidOperationException

c# - 禁用图像按钮上的回发单击 asp

c# - 重载com可见dll中的方法

asp.net - Request.Params ["key"] 做什么?

c# - 如何确定 Request.Form 对象有多大?

javascript - 如何在javascript或jquery中获取工作文件夹的物理路径?

c# - 通过 ASHX 服务查询 OLAP 多维数据集

handler - asp.net 中的 ASHX 处理程序文件有什么好处?