由 Response.End() 生成的异常
本文关键字:异常 Response End | 更新日期: 2023-09-27 18:34:16
我让用户使用以下代码下载文件。它工作正常,但也会产生错误,错误来源Response.End();
错误消息:无法计算表达式,因为代码已优化或本机帧位于调用堆栈之上。
下面是我的代码。我如何处理此错误,此错误是否没有释放我的资源,从而导致不必要的内存使用。
我将其用于 asp.net 网络表单应用程序。
try
{
string fileName = Request["file_ID"];
string path = Server.MapPath("~/App_Data/uploads/"+fileName);
//string = Server.MapPath(strRequest);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/"+file.Extension;
Response.WriteFile(file.FullName);
Response.End();
// System.Threading.Thread.Sleep(2000);
// Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('aa')", true);
}
else
{
//Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "myWindow.close();", true);
}
}
catch (Exception rt)
{
Response.Write(rt.Message);
}
}
我正在寻找这个解决方案,但我不确定如何在我的代码中实现它。
无法评估表达式...在网页上
更新:
我实际上希望用户下载文件并使用现在在代码中注释的脚本隐藏代码关闭文件。
所以我不确定如何更好地优化此代码以处理 respond.end 语句生成的异常。
我尝试使用HttpContext.Current.ApplicationInstance.CompleteRequest
但它也不起作用。
谢谢
另一个答案似乎是说,你可以不使用 Response.End,因为它是不必要的,或者为你当前捕获的 ThreadAbortException 添加一个捕获
该问题链接到另一个建议添加以下内容的问题:
// Sends the response buffer
Response.Flush()
// Prevents any other content from being sent to the browser
Response.SuppressContent = TrueResponse.SuppressContent = True
使用CC_4
或者尝试类似的东西
Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.Flush()
Response.End()
READ Response.End() 被认为是有害的吗?