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
    以流的方式 下载文件
  • 相关阅读:
    iOS设计模式:观察者
    Java面向接口编程小例子
    《The DeadLine》(《最后期限》) 读后感
    Codeforces Round #395 Div1的A题Timofey and a tree
    重写和强制转换再调用能编译但不能运行
    Java继承和静态-加载顺序
    C++之pair
    用Java面向对象思想实现一个微博的功能(未完)
    Java对象在内存图示
    Java中OOP对象和引用
  • 原文地址:https://www.cnblogs.com/clc2008/p/2342716.html
Copyright © 2011-2022 走看看