Pregunta

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);
¿Fue útil?

Solución

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.

Otros consejos

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.

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.

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?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top