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

    下载文件的代码

     /// <summary>
        /// 下载文件操作类
        /// </summary>
        public static class DownLoadFiles
        {
            /// <summary>  
            /// 下载文件
            /// </summary>
            /// <param name="strDownLoadFileName">要下载的文件名称</param>
            /// <returns></returns>
            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();",这个问题就自然的解决了。

  • 相关阅读:
    洛谷P3313&BZOJ-3531【SDOI2014】旅行--线段树动态开点+树链剖分
    BZOJ3932【CQOI2015】任务查询系统&洛谷P3168--主席树区间前K小+差分
    博客中的代码CE说明
    栗栗的书架--洛谷P2468【SDOI2010】&BZOJ 1926二分+前缀和+主席树
    hdu1010--Tempter of the Bone(迷宫)
    hdu1242 Rescue DFS(路径探索题)
    hdu 1241--入门DFS
    注意引用的用法
    划分树---hdu4417---区间查找(不)大于h的个数
    程序员能力矩阵
  • 原文地址:https://www.cnblogs.com/jasonwb/p/1875313.html
Copyright © 2011-2022 走看看