views:

1896

answers:

5

This code will always make my aspx page load twice. And this has nothing to do with AutoEventWireup.

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=data.pdf");
Response.BufferOutput = true;
byte[] response = GetDocument(doclocation);
Response.AddHeader("Content-Length", response.Length.ToString());
Response.BinaryWrite(response);
Response.End();

This code will only make my page load once (as it should) when I hardcode some dummy values.

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=data.pdf");
Response.BufferOutput = true;
byte[] response = new byte[] {10,11,12,13};
Response.AddHeader("Content-Length", response.Length.ToString());
Response.BinaryWrite(response);
Response.End();

I have also increased the request length for good measure in the web.config file.

<httpRuntime executionTimeout="180" maxRequestLength="400000"/>

Still nothing. Anyone see something I don't?

+1  A: 
GetDocument(doclocation);

May be this method somehow returns Redirection code ? or may be an iframe or img for your dynamic content?

If so:

In general the control could get called twice because of the url response. First it renders the content. After that your browser tries to download the tag (iframe,img) source which is actually a dynamic content that is generated. So it makes another request to the web server. In that case another page object created which has a different viewstate, because it is a different Request.

Can Erten
A: 

Quick Guess: Could it be that at this stage in the page life cycle, the class that contains GetDocument() has already gone through garbage collection? The ASP.NET Worker process then needs to reload the page in order to read that method again?

seanosteen
A: 

There is no iframe, its a straight aspx page. Nothing fancy.

The aspx page content is

GetDocument() is a static method and new variables are init when the method gets called so I don't think its a Garbage collection issue.

Sir Psycho
A: 

Have you tried it in the Page_Load ? and why is GetDocument a static method?

Greg B
A: 

Have you found a resolution to this yet? I having the same issue, my code is pretty much a mirror of yours. Main difference is my pdf is hosted in an IFrame.

So interesting clues I have found: If I stream back a Word.doc it only gets loaded once, if pdf it gets loaded twice. Also, I have seen different behavior from different client desktops. I am thinking that Adobe version may have something to do with it.

Update:

In my case I was setting the HttpCacheability to NoCache. In verifying this, any of the non client cache options would cause the double download of the pdf. Only not setting it at all (defaults to Private) or explicitly setting it to Private or Public would fix the issue, all other settings duplicated the double load of the document.

TheZenker