1

我有一个简单的HTTP处理程序,它允许我们的用户检索远程存储的PDF文件。 令人惊讶的是,当处理程序从IE9调用但IE8仍然卡住时,此代码运行良好。您必须再次调用该URL才能正确显示PDF。context.Response.Flush()不能在IE8上工作,但在IE9上正常工作

我在网上查了一下,看看是否有什么进一步处理响应。我最初以为的反应是不正确结束,却发现下面的文章: 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; 
      } 
     } 
    } 
} 

这儿还有没有人面临着同样类型的问题?

+0

[IE 8和客户端缓存](http://stackoverflow.com/questions/8348214/ie-8-and-client-side-caching)的可能重复也见http://stackoverflow.com/问题/ 9185678/ie7-8-pdf-file-wont-download-with-http-get – nemesv

回答

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为把我在正确的轨道上。

+0

谢谢!我有相同的问题,并加入'Response.ClearHeaders();'把它修正了。 –

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(); 
     } 
+0

感谢您的快速回答!我仍然想知道MSDN博客,我发现它不应该使用.End()和.Close()。 – Jaepetto

+0

那一定是在某些情况下,从未像现在这样使用一个页面内,因为它会导致'ThreadAbortException',你不会得到回应。该功能是有原因的,它有它的用途。 – nunespascal

相关问题