zoukankan      html  css  js  c++  java
  • 文件下载

    #region 下载文件
        /// <summary>
        
    /// 下载文件
        
    /// </summary>
        
    /// <param name="downName">客户端下载时候的名字 不需要文件后缀</param>
        
    /// <param name="serverPath">文件路径例如 根目录下 "../test/test.txt"</param>
        public static void DonwloadFile(string downName, string serverPath)
        {
            string filePath = serverPath;
            if (!serverPath.Contains(":"))
            {
                 filePath = HttpContext.Current.Server.MapPath(serverPath);//路径
            }
            string fileExtensioin = Path.GetExtension(filePath);
            string fileName = downName + fileExtensioin;//客户端保存的文件名
            
    //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            File.Delete(filePath);//此处用于导出文件夹后的删除,根据自己的需要
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            HttpContext.Current.Response.AddHeader("Content-Disposition""attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
        #endregion
    以流的方式 下载文件
  • 相关阅读:
    题解 P1030 【求先序排列】
    行列式及其打开方式
    题解 P2580 【于是他错误的点名开始了】
    题解 P1130 【红牌】
    题解 P5239 【回忆京都】
    题解 P1184 【高手之在一起】
    【笔记】自学ST表笔记
    题解 P1208 【[USACO1.3]混合牛奶 Mixing Milk】
    树状数组自学笔记
    EBS R12.2系统logo的修改
  • 原文地址:https://www.cnblogs.com/clc2008/p/2342716.html
Copyright © 2011-2022 走看看