5

Response.End() generates ThreadAbortException.

Using HttpContext.Current.ApplicationInstance.CompleteRequest in place of it doesn't solve the problem.

So, can we use Response.Flush() instead of Response.End()

Aristos
  • 66,005
  • 16
  • 114
  • 150
user1509
  • 1,151
  • 5
  • 17
  • 45

3 Answers3

11

The Response.Flush() is send to the browser what is on buffer, but is not stop the processing of the page, so it will continue the execution of the next steps.

What is Response.End() do is to stop the execution steps on the next function call in the asp.net Page Life Cycle. http://msdn.microsoft.com/en-us/library/ms178472.aspx

And that's why it need to throw an exception, to throw out of the loop of the series of call.

One alternative is the Response.Close() but this also is not stop the execution steps and probably you have some other error if you try to send something after the close of the connection with the browser. if you do Response.Close() you need to also do Flush() just before you call it or else browser gets unpredictable data.

you can also read : Redirect to a page with endResponse to true VS CompleteRequest and security thread

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
4

Use the condition before download the file Response.IsClientConnected -

if (Response.IsClientConnected)
 {
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "Application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.TransmitFile(Server.MapPath(@"yourpath" + fileName));
    Response.Flush();
    Response.Close();
  }

It's working for me well after lot of struggle. I hope it works for you too.

slm
  • 15,396
  • 12
  • 109
  • 124
Viishnuu
  • 69
  • 2
0

No! You cannot! You can

Response.SuppressContent = true;
Pit J
  • 169
  • 8
  • 1
    You should elaborate why you cannot and your solution in your answer. – Florian Blume Mar 13 '19 at 12:46
  • Read about Flush() help. This will counting to load the page content and will not send interaction to browser. Only SuppressContent = true and End() will do that. – Pit J Mar 13 '19 at 13:07