2

I have a ASP.NET app that at one point generates a PDF file and loads the next page. I can easily do this with two separate buttons but it is made much tougher when I try to do this with one button.

When both are fired by the same button the PDF will download but the page will not load. I even had the thread sleep after the file was transmitted but it would wait but then stop afterwards.

I have attached the code that I have been trying to make work:

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=labels.pdf");
Response.TransmitFile(Server.MapPath("~/"+randomNumber.ToString()+".pdf"));
Server.Transfer("~/createshipment.aspx", true);
user7116
  • 63,008
  • 17
  • 141
  • 172
Joe Tyman
  • 1,417
  • 5
  • 28
  • 57
  • 1
    You can try Response.Write instead of Server.Transfer. You could also move to the next page and init. the download from there. – user1231231412 Dec 30 '11 at 19:30

4 Answers4

1

You can't have two different responses from the server but you're trying to do so.

First - you'd like the server to return a PDF. Second - you'd like the server to return the createshipment.aspx page.

This is just against the communication protocol. Probably the best solution is already presented by another user, competent_tech - you could open a new window (javascript's window.open) and this new window would return the PDF and in the same time the main window could post to the server and be redirected to the createshipment.aspx.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
1

So in a nutshell you want to navigate to the next page that says something like "thank you for downloading this file" and start the download.

What you should do is on your button click you need to generate PDF and save it somewhere (on disk or DB - whichever is easier in your app), store the name/location of the new file (or primary key from DB) in a session variable and redirect to the next page. No reason to do transfer here. Then on that next page you should add a hidden iframe that points to your saved file.

Alternatively your button click could be just a link to the next page, which includes a hidden iframe pointing to the page that generates PDF. This is a bit simple but wouldn't work so well if you need to pass parameters from original page to the page that generates PDF.

Ilia G
  • 10,043
  • 2
  • 40
  • 59
0

I know this is old, but I'm just seeing it (looking for similar info myself).

I'm going to guess that this is causing issues:

Response.TransmitFile(Server.MapPath("~/"+randomNumber.ToString()+".pdf"));

You would need to map the path to the actual file and not some randomly created filename - or am I missing some steps?

  • That is really not an issue. The file is being created on the fly earlier in the code. randomNumber returns the same number while in scope and sometimes out of scope. – Joe Tyman Feb 16 '13 at 16:19
0

This is because server.transfer "...terminates execution of the current page and starts execution of a new page by using the specified URL path of the page".

Your best bet is to open a new window in the client that gets the PDF and then perform whatever postback is needed to move the user to the next page.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • why does it download the file, sleep, and then stop loading the next page. – Joe Tyman Dec 30 '11 at 19:38
  • That does seem like odd behavior. Are there any exceptions being reported to the event log? It almost sounds like there is an error on the page itself. You could look at the source of the page and see where it stops writing; that might give you a clue. – competent_tech Dec 30 '11 at 19:45