I have a button that, when clicked, will generate a PDF file and write it in response.
This is the basic code structure:
try
{
using(Stream stream = generatePdf())
{
var file = createFile(stream);
file.Transmit(HttpContext.Current.Response);
}
}
catch (Exception ex)
{
}
The transfer 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 does not complete the postback.
If I selected the exception above file.Transmit, error handling would work correctly, and I would see that the message would be displayed in my browser. However, if file.Transmitthere is an exception afterwards , then nothing happens. The user saves / opens the PDF file, and the page does not reload.
How can I make postback always end so that I can display the corresponding message to the user?
source