【问题标题】:ASHX image handler works with chrome, not IE8ASHX 图像处理程序适用于 chrome,而不是 IE8
【发布时间】:2010-07-25 01:16:24
【问题描述】:

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

}

【问题讨论】:

标签: c# asp.net ashx


【解决方案1】:

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

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

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

context.Response.ContentType = getContentType(imageFileName);

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2013-09-03
      • 1970-01-01
      相关资源
      最近更新 更多