• 文件下载类


    /// <summary>
        /// 文件下载类
        /// 调用1:DownLoadFile("/2003.xls", null);
        /// 调用2:DownLoadFile("/2003.xls", "");
        /// 调用3:DownLoadFile("/2003.xls", "temp.xls");
        /// </summary>
        /// <param name="URL">文件路径(格式:/upload/2003.xls)</param>
        /// <param name="filename">自定义文件名(不想自定义文件名请传[ "" or null ] )</param>
        public void DownLoadFile(string URL, string filename)
        {
            string filePath = System.Web.HttpContext.Current.Server.MapPath("./") + URL;//路径
            if (string.IsNullOrEmpty(filename))
                filename = System.IO.Path.GetFileName(filePath);
            //以字符流的形式下载文件
            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename));
            System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }

    作者:达奇
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    雷霆战机
    各种 Python 库/模块/工具
    redis
    25
    为什么Python中“2==2>1”结果为True
    thinkphp3.2路由美化,url简化
    thinkphp调整框架核心目录think的位置
    thinkphp3.2中开启静态缓存后对404页面的处理方法
    thinphp中volist嵌套循环时变量$i 被污染问题,key="k"
    thinkphp中如何是实现多表查询
  • 原文地址:https://www.cnblogs.com/dachie/p/1835995.html
Copyright © 2020-2023  润新知