0

I have a process that sends a PDF to the user via

Response.BinaryWrite(pdf);

After this completes, I want to

Response.Redirect("Default.aspx");

The problem is that the redirect fires before the BinaryWrite() has completed. I know this because I do not see the popup to download the PDF as I should.

If I remove

Response.Redirect("Default.aspx");

Then I get the popup as I should. So, there's a race condition here (probably has something to do with HTTP being connectionless). I could do

Thread.Sleep(5000);

But that is ridiculous. Is there a good solution to this problem?


I tried AJAX. This isn't working either. Due to an issue with AJAX and popups, I have to use the workaround
window.frames["#pdfPopup"].location.reload();

To get my popup to display.

Then this code never runs...

alert('before href');//no alert ever displays
window.location.href = "http://www.yahoo.com/";

This could be because I called reload(). Any ideas?


I hooked into onload for the frame, but this still doesnt work! Ugh. I read that onload does not fire for frames that contain activeX controls, including PDF. I read this on SO and multiple google sites.
<iframe id="pdfPopup" style="visibility:hidden;height:0px;width:0px;" onload="Redirect();"></iframe>

onload does fire on page load, but not when I call reload and put a PDF in there. So I have hit a brick wall. Anybody have any solutions for this?

Community
  • 1
  • 1
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

2 Answers2

2

You can't use Response.BinaryWrite with Response.Redirect because the user agent will handle the redirect's HTTP 302 response and act accordingly. What you should do is use one or the other. Using a Response.BinartyWrite (as long as nothing fails and you haven't manually set the response code) will return a HTTP 200 along with the content once the response has ended.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • So are you saying that it is not possible to show a popup to download a PDF file and then redirect the user back to their home page? – P.Brian.Mackey May 27 '11 at 15:45
  • 1
    It's certainly possible, but you need the client to handle the showing of the popup. Probably do a `document.location.href` set to the PDF download uri, and then a `document.location.href` set to the homepage url. – Matthew Abbott May 27 '11 at 15:47
  • Ok I follow you now. I'll try AJAX. – P.Brian.Mackey May 27 '11 at 15:54
  • Note that I also tried your suggestion for `document.location.href`. This resulted in the page redirecting before the popup ever showed up. So I'm back to where I started. – P.Brian.Mackey May 27 '11 at 17:39
2

Instead of using document.href open the pop-up window through window.open on client side and pass the URL to download the PDF there. Then you can perform the redirect

window.open("PDFService.aspx?param1=...");
document.location.href = "Default.aspx";
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
Denis Mazourick
  • 1,425
  • 2
  • 13
  • 24
  • +1 @Denis Mazourick This sounds promising. I am not sure how I can do any kind of thread sync being that Response.BinaryWrite() doesn't signal when it is complete (if I could I wouldn't have this issue to start with). Are you suggesting that I write my own serializer instead of using BinaryWrite()? Not quite sure how to attack that if that's the case. – P.Brian.Mackey May 27 '11 at 15:50
  • Just add the thread sync code right below the BinaryWrite. Be sure to use locks. NOTE: What I'm suggesting is combination of client side and server side. Not just a server side. And once again - you redirect user using client side code, not a server side and ONLY after the queue item got the "completed" state. You may end up with calling server multiple times from client until you will get that. – Denis Mazourick May 27 '11 at 15:52
  • I see what you are saying and I've done plenty of thread sync app's in the past. I'm unsure how locks/mutexes can be used to solve the problem given my situation. There's nothing to lock on. HTTP is a connectionless protocol. – P.Brian.Mackey May 31 '11 at 18:21
  • 1
    @P.Brian.Mackey - Why you just don't open the pop-up window through window.open on client side and pass the URL to download the PDF there? Then the page on the server side will just BinaryWrite data. Be sure to set the ContentType header to application/pdf. It will automatically propose user to either open the PDF or save it to disk and will automatically close that popup window. So, your user won't be redirected from the page anywhere and you will not need client side redirects – Denis Mazourick May 31 '11 at 22:01
  • GREAT idea. For real, I've been struggling with this for a few days. I've tried ajax, document.href, server side code...and this is the only thing that worked. Thanks a million. – P.Brian.Mackey Jun 01 '11 at 13:44