代码之家  ›  专栏  ›  技术社区  ›  jhunter

.aspx page Response.BinaryWrite IE7上的图像

  •  2
  • jhunter  · 技术社区  · 17 年前

    我维护一个应用程序,它有一个.aspx页面,从数据库加载图像,并使用Response.BinaryWrite()将其写回客户端。不久前,这种方法还很有效。两件事发生了变化,我们将应用程序升级到.NET3.5,他们将所有工作中的计算机升级到IE7。

    Firefox上的一切都很好,但我在IE7中得到的只是一个红色的X。所以我认为这个问题与IE7有关?是否有安全设置阻止它从.aspx表单加载图像?它已设置为基于内容类型而不是扩展名显示。

    <asp:image id="imgContent" runat="server" Visible="true" ImageUrl="ProductContentFormImage.aspx"></asp:image>
    
        protected void Page_Load(object sender, System.EventArgs e)
        {
            Hashtable hshContentBinary = (Hashtable)Session["hshContentBinary"];
            byte[] content = (byte[]) hshContentBinary["content"];
            string extension = (string) hshContentBinary["extension"];
    
    
            string contentTypePrefix = "application";
            switch(extension.ToLower())
            {
                case "gif":
                case "jpg":
                case "bmp":
                    contentTypePrefix = "image";
                    break;
                case "tif":
                    contentTypePrefix = "image";
                    break;
                case "tiff":
                    contentTypePrefix = "image";
                    break;
                case "eps":
                    contentTypePrefix = "image";
                    break;
                default:
                    Response.AppendHeader( 
                        "Content-disposition",
                        "attachment; filename=content." + extension );
                    break;
            }
            Response.ContentType = contentTypePrefix + "/" + extension;
            Response.BinaryWrite(content);
        }
    

    编辑:

    好的,我遵循了你的建议,通过更多的研究,我将方法改为以下,但仍然不起作用。

    protected void Page_Load(object sender, System.EventArgs e)
    {
        Hashtable hshContentBinary = (Hashtable)Session["hshContentBinary"];
        byte[] content = (byte[]) hshContentBinary["content"];
        string extension = (string) hshContentBinary["extension"];
        string contentType;
        string contentDisposition = "inline; filename=content." + extension;
    
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Clear();
    
        switch(extension.ToLower())
        {
            case "gif":
                contentType = "image/gif";
                break;
            case "jpg":
            case "jpe":
            case "jpeg":
                contentType = "image/jpeg";
                break;
            case "bmp":
                contentType = "image/bmp";
                break;
            case "tif":
            case "tiff":
                contentType = "image/tiff";
                break;
            case "eps":
                contentType = "application/postscript";
                break;
            default:
                contentDisposition = "attachment; filename=content." + extension;
                contentType = "application/" + extension.ToLower();
                break;
        }
    
        Response.Buffer = true;
        Response.Expires = 0;
        Response.ContentType = contentType;
        Response.AddHeader("Content-Length", content.Length.ToString());
        Response.AddHeader("Content-disposition", contentDisposition);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(content);
        Response.End();
    }
    
    10 回复  |  直到 17 年前
        1
  •  2
  •   Guffa    17 年前

    您的mime类型不正确。至少对于图像而言,这样做效果更好:

    string contentType = "application/" + extension.ToLower();
    switch(extension.ToLower()) {
       case "gif": contentType = "image/gif"; break;
       case "jpg":
       case "jpeg":
       case "jpe": contentType = "image/jpeg"; break;
       case "bmp": contentType = "image/bmp"; break;
       case "tif":
       case "tiff": contentType = "image/tiff"; break;
       case "eps": contentType = "application/postscript"; break;
       default:
          Response.AppendHeader( 
             "Content-disposition",
             "attachment; filename=content." + extension );
          break;
    }
    Response.ContentType = contentType;
    

    Hera是mime类型和文件扩展名,如果您需要添加更多: http://www.w3schools.com/media/media_mimeref.asp

        2
  •  1
  •   Sachin    17 年前

    1) 在设置ContentyType之前,请尝试执行Response.Clear(),最后执行Response.end。 2) 确保会话中的扩展名没有句点(.),即对于.gifs应该是gif,对于.jpg应该是jpg,等等。

        3
  •  1
  •   Ira Miller    17 年前

    考虑设置一些缓存,包括客户端缓存控制头。执行此操作时,请确保为每个图像设置不同的文件名

    contentDisposition = String.Format("attachment; filename=content{0).{1}",fileName, fileExtension);
    

    和客户端缓存头

    context.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(YourDatabaseImageOrConfigFile.CacheSeconds));
    context.Response.Cache.SetOmitVaryStar(true);
    context.Response.Cache.SetLastModified(YourDatabaseImage.ModifiedDate);
    

    我会将这个页面变成一个ASHX http处理程序,以消除页面生命周期的开销。

    我已经把这整件事写了好几次了,如果需要的话,我可以提供代码。此映像端点位于一个站点上,每秒执行大约80个请求。

        4
  •  0
  •   Eddie    17 年前

    Response.ClearContent();
    Response.ClearHeaders();
    

    ……就在。。。

    Response.ContentType = contentTypePrefix + "/" + extension;
    

    我们必须这样做才能在IE7中获得正确的文件类型关联。

        5
  •  0
  •   Chris Van Opstal    17 年前

    尝试将内容配置设置为内联而不是附件,如:

    Response.AppendHeader( 
       "Content-disposition",
       "inline; filename=content." + extension );
    

    虽然大多数浏览器在这方面都非常灵活,“附件”旨在要求用户采取进一步的行动。 IETF RFC 1806

    附件的显示是 收件人,而内联消息 组件将自动显示 查看消息时。

        6
  •  0
  •   AnthonyWJones    17 年前

    Fiddler 因此,您可以看到对图像URL的响应。

    如果可能,将ASPX改为ASHX。

        7
  •  0
  •   shahkalpesh    17 年前

    尝试访问IE7中的其他网站。

    如果它不显示图像,我猜您可能会将某些设置(例如web开发者工具栏或fiddler)设置为“不加载图像”。

        8
  •  0
  •   Chris    17 年前

    下面是我使用的代码,它适用于所有浏览器。

    protected void Page_Load(object sender, EventArgs e)
    {
      byte[] jpg = .....
    
      Response.Clear();
      Response.ContentType = "image/jpeg";
      Response.BinaryWrite(jpg);
      Response.End();
    }
    
        9
  •  0
  •   jhunter    17 年前

    图像被破坏,IE7无法显示,但Firefox可以。图像太大,无法显示在屏幕上,我也看不到它被切断的位置。

    谢谢你的建议。

        10
  •  0
  •   Jeff Ancel    16 年前

    我不是100%确定IE7,但是我在ie8上遇到了类似的问题。我的问题的解决方案是确保我涵盖了“image/pjpeg”的内容类型。不知道你需要多少不同的保险,但这一次解决了我的问题。