I have a button that when clicked, will generate a PDF and write it out to the response.
This is the basic structure of the code:
try
{
using(Stream stream = generatePdf())
{
var file = createFile(stream);
file.Transmit(HttpContext.Current.Response);
}
}
catch (Exception ex)
{
// Handle exception
// Display Error Message
}
The transmit method contains the following code:
response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", filename));
response.AddHeader("Content-Length", bytes.Length.ToString());
response.ContentType = "application/octet-stream";
response.BinaryWrite(bytes);
Downloading the file works fine, except that it doesn't complete the postback.
If I were to throw an exception above file.Transmit
, the error handling would work properly and I would see the message get displayed in my browser. However, if there is an exception after the file.Transmit
then nothing happens. The user saves/opens the pdf and the page does not reload.
How can I make it so that the postback always completes, that way I can display an appropriate message to the user?