1

I have a simple HTTP handler that allows our users to retrieve a PDF file stored remotely. Surprisingly, this code works well when the handler is called from IE9 but IE8 stays stuck. You have to call the url a second time to get the pdf displayed correctly.

I checked on the web to see if there was something further to do with the response. I initially thought that the response was not properly ended, but found the below article: http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx Apparently, the context.Response.Close() or the context.Response.End() should not be used.

The code I am using:

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;
            }
        }
    }
}

Did anyone faced the same type of issue?

E. Jaep
  • 2,095
  • 1
  • 30
  • 56
  • possible duplicate of [IE 8 and client-side caching](http://stackoverflow.com/questions/8348214/ie-8-and-client-side-caching) also see http://stackoverflow.com/questions/9185678/ie7-8-pdf-file-wont-download-with-http-get – nemesv Sep 03 '12 at 18:27

2 Answers2

2

Clearing the headers before setting the content type did the trick:

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

Thanks anyway to @nunespascal for putting me on the right track.

E. Jaep
  • 2,095
  • 1
  • 30
  • 56
0

Call a Response.End.

That will end your response, and tell the browser that all content is received.

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();
        }
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • Thanks for the quick answer! I'm still wondering about the MSDN blog I found stating that both .End() and .Close() should never be used. – E. Jaep Sep 03 '12 at 18:39
  • That must be in some context, like never use it inside a page, cause it would cause a `ThreadAbortException`, and you would not get a response. The function is provided for a reason, it has its uses. – nunespascal Sep 03 '12 at 18:45