我正在编写一个简单的测试页面,在按钮点击下载文本文件到浏览器。我得到一个非常奇怪的错误,我从来没有见过。有什么想法吗?
错误发生在’Response.End();’并且文件永远不会访问客户端浏览器
码:
string filePath = "C:\\test.txt"; FileInfo file = new FileInfo(filePath); if (file.Exists) { Response.ClearContent(); Response.AddHeader("Content-Disposition","attachment; filename=" + file.Name); Response.AddHeader("Content-Length",file.Length.ToString()); Response.ContentType = "text/plain"; Response.TransmitFile(file.FullName); Response.End(); }
错误:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
解决方法
尝试改变它。
Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition","attachment; filename=" + file.Name); Response.AddHeader("Content-Length",file.Length.ToString()); Response.ContentType = "text/plain"; Response.Flush(); Response.TransmitFile(file.FullName); Response.End();