Is there a way to end recording after writing the file in response?

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)
{
    // Handle exception
    // Display Error Message
}

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?

+3
source
2

, , : PDF. . , ( JavaScript, ), , JavaScript .

HTTP- .

+4

Response.End() .

, , :

1) .

2) ( AJAX ) iframe ... response.end , .

EDIT: Response.TransmitFile

+1

Source: https://habr.com/ru/post/1788595/


All Articles