开发者

ASP.NET - Stop/Workaround Response.Write()'s asynch behavior

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 t开发者_StackOverflowhe 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜