【问题标题】:File not downloaded when Response.End() is replaced by ApplicationInstance.CompleteRequest()当 Response.End() 被 ApplicationInstance.CompleteRequest() 替换时文件未下载
【发布时间】:2019-06-10 07:10:07
【问题描述】:

我想创建一个文件下载。这是它的原始代码:

Response.Clear()
Response.ContentType = "text/html"
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=""Dashboard_{0}.html""", time))
Response.Write(html)
Response.End()

看起来不错。文件已正常下载,但我在输出中发现以下消息。

在 mscorlib.dll 中发生了“System.Threading.ThreadAbortException”类型的第一次机会异常

我试图寻找解决这个异常的方法,我看到每个人都在谈论用HttpContext.Current.ApplicationInstance.CompleteRequest()替换Response.End()

当我尝试时,异常不再发生,但结果更糟糕 - 更改后文件没有下载。就像我只是删除 Response.End() 并且什么都不添加一样。

在这种情况下,我应该怎么做才能摆脱需要下载的异常?还是应该忽略该异常,因为它似乎不会产生任何不利的结果?

【问题讨论】:

    标签: asp.net download threadabortexception


    【解决方案1】:

    这里是 response.End()

    为了模仿 ASP 中 End 方法的行为,该方法试图 引发 [ThreadAbortException] 异常。

    请看这个 https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.end?redirectedfrom=MSDN&view=netframework-4.8#System_Web_HttpResponse_End

    也尝试删除

    Response.Clear()

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 2020-12-15
      • 2014-01-26
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多