下载文件时报错 "由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值" 解决方法

分类:.NET
 标签:.NET,,无法计算表达式的值,Response.End
   修改 | 阅读(680)| 评论(2)


/// 下载文件操作类
public static class DownLoadFiles
{
 /// 下载文件
 /// 要下载的文件名称
 public static void DownLoadFile(string strDownLoadFileName)
 {
  if (String.IsNullOrEmpty(strDownLoadFileName))return;
  try
  {
   FileInfo info = new FileInfo(strDownLoadFileName);
   HttpContext.Current.Response.Clear();
   HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(info.Name));
   HttpContext.Current.Response.AddHeader("Content-Length", info.Length.ToString());
   HttpContext.Current.Response.ContentType = "application/octet-stream";//ms-excel
   HttpContext.Current.Response.WriteFile(info.FullName);
   //HttpContext.Current.Response.End();
   HttpContext.Current.ApplicationInstance.CompleteRequest();
  }
  catch (Exception ex)
  {
   throw ex;
  }
 }
}




// 最初是 HttpContext.Current.Response.End() 总是报错,异常“由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值”后来将上面的"HttpContext.Current.Response.End();"改成"HttpContext.Current.ApplicationInstance.CompleteRequest();",这个问题就自然的解决了。


上一篇:无
【xylw】评论于:2014-10-30 12:08:48    [回复本评论]

Response.End();

CompleteRequest()

【xylw】于14-10-30 12:09回复
您的昵称:*
QQ登录(无需注册直接登录可进行回复)
您的邮箱:(填写邮箱,如有回复可进行邮件通知)
验证码:
点击刷新