2

私のアプリケーションでは、ファイルをダウンロードする必要があるため、次のコードを使用しています。

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

Response.End()私はエラーが発生していますThreadAbortException

このエラーを回避するために、httpApplication.CompleteRequest() を使用しようとしていますが、これも使用できません。

httpApplication.CompleteRequest() を使用したコードは以下のとおりです。

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

HttpApplication.CompleteRequest() を使用すると、このエラーが発生します

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

私の疑問を明確にすることができれば幸いです...助けてください....

4

5 に答える 5

6

Response.End()をスローすると予想されThreadAbortExceptionます。
これは仕様によるもので、残りのページ応答は処理されません。

この例外が発生しても問題はなく、ページがそれ以上処理されないことが保証されます。

参照: HttpResponse.End

CompleteRequest メソッドは例外を発生させず、CompleteRequest メソッドの呼び出しの後のコードが実行される可能性があります。後続のコードの実行を回避することが目的であり、End のパフォーマンス ペナルティが許容できる場合は、CompleteRequest の代わりに End を呼び出すことができます。

于 2013-06-24T08:18:28.973 に答える
3

このコードを試してください:

Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename =" + strFileName + ".xls");
Response.TransmitFile(strFilePath);
HttpContext.Current.ApplicationInstance.CompleteRequest();
于 2013-06-24T07:51:44.413 に答える