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

写一个zip文件在IE7中不起作用

  •  3
  • jhunter  · 技术社区  · 15 年前

    我继承了一个旧的应用程序,它在数据库中存储一个zip文件,需要检索这个文件。在firefox中,我可以打开zip,其中的每个文件都可以。当我在IE7中运行它时,会得到以下错误。

    Internet Explorer无法从本地主机下载productContentForImage.aspx。

    Internet Explorer无法打开此Internet站点。请求的站点不可用或找不到。请稍后再试。

    我正在使用下面的代码。

    byte[] content = (byte[])Session["contentBinary"];
    
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Clear();
    
    Response.Buffer = true;
    Response.Expires = 0;
    Response.ContentType = "application/zip";
    Response.AddHeader("Content-Length", content.Length.ToString());
    Response.AddHeader("Content-Disposition", "attachment; filename=content.zip");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(content);
    Response.End();
    
    2 回复  |  直到 12 年前
        1
  •  6
  •   Ryan Brunner    15 年前

    这是一个特别针对IE的奇怪的小bug。

    基本上,当您将到期时间设置为0时,问题就会出现。

    IE基本上经历了以下过程:

    1. IE确定文件是要“下载”的,这会导致IE打开文件下载弹出窗口。

    2. 一旦用户单击“打开”或“保存”,IE就会尝试下载该文件,但由于该文件设置为立即过期,因此IE会出错。

    将到期时间设置为一个小的非零数字,比如1分钟,您应该看到问题消失了。

        2
  •  1
  •   musefan    12 年前

    我发现将httpcacheability设置为private可以修复此问题

    context.Response.Cache.SetCacheability(HttpCacheability.Private);