【问题标题】:context.Response.Flush() not working on IE8 but working properly on IE9context.Response.Flush() 在 IE8 上不起作用,但在 IE9 上正常工作
【发布时间】:2012-08-28 11:10:24
【问题描述】:

我有一个简单的 HTTP 处理程序,它允许我们的用户检索远程存储的 PDF 文件。 令人惊讶的是,当从 IE9 调用处理程序但 IE8 卡住时,此代码运行良好。您必须再次调用 url 才能正确显示 pdf。

我在网上查了一下,看看是否还有其他与回复有关的事情。我最初以为响应没有正确结束,但发现以下文章: http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx 显然,不应使用 context.Response.Close() 或 context.Response.End()。

我正在使用的代码:

using System.Web;

namespace WebFront.Documents
{
    public class PDFDownloader : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            // Makes sure the page does not get cached
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            // Retrieves the PDF
            byte[] PDFContent = BL.PDFGenerator.GetPDF(context.Request.QueryString["DocNumber"]);

            // Send it back to the client
            context.Response.ContentType = "application/pdf";
            context.Response.BinaryWrite(PDFContent);
            context.Response.Flush();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

有人遇到过同样的问题吗?

【问题讨论】:

标签: c# asp.net internet-explorer-8 internet-explorer-9 httphandler


【解决方案1】:

在设置内容类型之前清除标题就可以了:

public void ProcessRequest(HttpContext context)
{
    // Makes sure the page does not get cached
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    // Retrieves the PDF
    byte[] PDFContent = BL.PDFGenerator.GetPDF(context.Request.QueryString["DocNumber"]);

    // Send it back to the client
    context.Response.ClearHeaders();
    context.Response.ContentType = "application/pdf";
    context.Response.BinaryWrite(PDFContent);
    context.Response.Flush();
    context.Response.End();
}

感谢@nunespascal 让我走上正轨。

【讨论】:

  • 谢谢!我遇到了完全相同的问题,并添加了Response.ClearHeaders(); 修复了它。
【解决方案2】:

致电Response.End

这将结束您的响应,并告诉浏览器已收到所有内容。

public void ProcessRequest(HttpContext context)
        {
            // Makes sure the page does not get cached
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            // Retrieves the PDF
            byte[] PDFContent = BL.PDFGenerator.GetPDF(context.Request.QueryString["DocNumber"]);

            // Send it back to the client
            context.Response.ContentType = "application/pdf";
            context.Response.BinaryWrite(PDFContent);
            context.Response.Flush();
            context.Response.End();
        }

【讨论】:

  • 感谢您的快速回答!我仍然想知道我发现的 MSDN 博客指出 .End() 和 .Close() 都不应使用。
  • 这必须在某些情况下,比如永远不要在页面中使用它,因为它会导致ThreadAbortException,并且您不会得到响应。提供该功能是有原因的,它有它的用途。
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多