context.Response.Flush()不能在IE8上工作,但在IE9上正常工作

时间:2012-09-03 18:20:57

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

我有一个简单的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;
            }
        }
    }
}

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

2 个答案:

答案 0 :(得分:2)

在设置内容类型之前清除标题可以解决问题:

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让我走上正轨。

答案 1 :(得分:0)

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