【问题标题】:How to display PDF attachment in IE6如何在 IE6 中显示 PDF 附件
【发布时间】:2010-02-23 22:40:01
【问题描述】:

我有一个 ASP.net 应用程序,它向客户端返回一个二进制 PDF 文件(从数据库中存储,之前已上传)。

我的代码适用于所有浏览器,除了 Internet Explorer 6(我的生活故事!)。在 IE6 中,当用户点击打开时,Adobe 报错:“打开此文档时出错。找不到文件。”

当用户点击保存时,PDF 保存完好,双击打开即可。我难住了。谷歌已经给出了缓存的建议(将缓存控制设置为私有等),我已经尝试了所有这些但没有任何效果。

奇怪的行为是当我在我的 ASP .net 服务器层(使用 NFop)从头开始生成 PDF 时,IE 会很好地打开它(使用相同的代码!)。

这是我通过网络发送二进制数据的代码:

    // Firefox doesn't like spaces in filenames.
    filename = filename.Replace(" ", "_");

    string extension = Path.GetExtension(filename);

    string contentType;

    switch (extension)
    {
        case "pdf":
            contentType = "application/pdf";
            break;
        default:
            contentType = "application/x-unknown";
            break;
    }

    context.Response.Clear();
    context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
    context.Response.Charset = "";
    context.Response.ContentType = contentType;
    context.Response.BinaryWrite(data);
    context.Response.Flush();

以下是应用程序版本:

  • ASP .net 3.5
  • IE6.0.2900.2180.xpsp_s
  • 列表项
  • p2_gdr.091208-2028
  • Adobe Reader 8.0.0 版
  • Windows XP SP 2
  • SQL Server 2008

任何帮助/建议将不胜感激。谢谢:)

编辑:

我已插入 Fiddler 以查看标头,果然这似乎是一个缓存问题。咕噜!

上传我的 NFop PDF(有效的 PDF)时,它正在发送 cache-control = private。 当我的附件 PDF(不起作用的那个)被上传时,它会发送 no-cache。

我查看了 Response 对象,当我调用 context.Response.Flush() 时,它们似乎具有相同的标头。

还是一头雾水!

已解决

我们框架中的某个地方是一个使用反射调用的流氓方法:

/// /// 设置请求的过期时间并强制不缓存 /// 受保护的无效 SetCacheExpiration(HttpContext 上下文) { //设置缓存立即过期 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetSlidingExpiration(true); context.Response.Cache.SetExpires(DateTime.Now); context.Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0)); context.Response.Cache.SetNoStore(); context.Response.Cache.SetAllowResponseInBrowserHistory(false); context.Response.Cache.SetValidUntilExpires(false); context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

}

感谢您的帮助,缓存!有趣的是,唯一没有缓存下载(打开时)的浏览器是 IE6。

【问题讨论】:

  • 你应该打电话给Path.GetExtension(fileName)
  • 请注意Path.GetExtension(fileName) 将包含.
  • @Tom 感谢您的撇号! :D

标签: asp.net pdf internet-explorer-6


【解决方案1】:

这里the method I've used before to render in-browser PDFs 用于 IE6。再次...这是针对我们在浏览器中显示 PDF 的项目,但它应该为您提供在阅读器中显示的 PDF。

【讨论】:

    【解决方案2】:

    我们框架中的某个地方是一个使用反射调用的流氓方法:

        /// <summary>
        /// Sets the expiratoin of the request and force no cache
        /// </summary>
        protected void SetCacheExpiration(HttpContext context)
        {
            //sets the cache to expire immediately
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetSlidingExpiration(true);
            context.Response.Cache.SetExpires(DateTime.Now);
            context.Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0));
            context.Response.Cache.SetNoStore();
            context.Response.Cache.SetAllowResponseInBrowserHistory(false);
            context.Response.Cache.SetValidUntilExpires(false);
            context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    
        }
    

    感谢您的帮助,缓存!有趣的是,唯一没有缓存下载(打开时)的浏览器是 IE6。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多