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
    以流的方式 下载文件
  • 相关阅读:
    CF526D Om Nom and Necklace
    POJ2406 Power Strings
    POJ3461 Oulipo
    luogu P1341 无序字母对
    UOJ 117 欧拉回路
    骑马修栅栏
    vimdiff env.txt export.txt set.txt
    Linux自动执行任务
    消灭 Bug!推荐5款测试员不可不知的bug管理工具!
    Bugzilla使用手册及解决方案
  • 原文地址:https://www.cnblogs.com/clc2008/p/2342716.html
Copyright © 2011-2022 走看看