salah9
salah9

Reputation: 502

Response.End() throwing an Error

In my application I need to download a file, So I am using this piece of code:

 Response.ContentType = "application/octet-stream";                    
 Response.AppendHeader("Content-Disposition", "attachment; filename =" + strFileName + ".xls");
 Response.TransmitFile(strFilePath);
 Response.End();

at Response.End() i am getting an error ThreadAbortException

To aviod this error I am trying to use httpApplication.CompleteRequest(), but i m not able to use this too.

The code with httpApplication.CompleteRequest() is below,

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename =" + strFileName + ".xls");
Response.TransmitFile(strFilePath);
HttpApplication.CompleteRequest();

I m getting this error when i m using HttpApplication.CompleteRequest()

An object reference is required for the non-static field, method, or property 'System.Web.HttpApplication.CompleteRequest()'

I hope i m able to make my doubt clear... help me out....

Upvotes: 2

Views: 15402

Answers (5)

hakan baştürk
hakan baştürk

Reputation: 21

I have experienced this problem and have solved it with the following code

ScriptManager sm = ScriptManager.GetCurrent(this.Page);
              sm.RegisterPostBackControl(this.grid);

Upvotes: 0

rakeshyadvanshi
rakeshyadvanshi

Reputation: 293

Just comment this line and you're good to go:

//Response.End();

It worked for me :)

Upvotes: -1

Ivan Renteria Vidal
Ivan Renteria Vidal

Reputation: 39

The error occures because you are using a ASP update panel or any control using JavaScript. Try to use control native from ASP or HTML without JavaScript or ScriptManager or scripting.

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

Response.End() is expected to throw a ThreadAbortException.
This is by design, so that the rest of the page response is not processed.

It is perfectly ok to get this exception, and it will ensure the page is not processed further.

Refer: HttpResponse.End

The CompleteRequest method does not raise an exception, and code after the call to the CompleteRequest method might be executed. If your intention is to avoid execution of subsequent code, and if the performance penalty of End is acceptable, you can call End instead of CompleteRequest.

Upvotes: 6

Sanjeev Rai
Sanjeev Rai

Reputation: 6992

Try this code:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename =" + strFileName + ".xls");
Response.TransmitFile(strFilePath);
HttpContext.Current.ApplicationInstance.CompleteRequest();

Upvotes: 3

Related Questions