• 文件下载


    #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
    以流的方式 下载文件
  • 相关阅读:
    codespell
    IDEA打开就闪退或关闭详细解决办法
    git log 空格 翻页
    Vue+ElementUI 分页器elpagination的使用方法
    Vue中computed用法 computed比较适合对多个变量或者对象进行处理后返回一个结果值 计算属性
    word如何删除文本中多余的空行
    Notepad++删除空白行
    Vue生命周期详解
    elementui 自定义树形控件特定行里字体的颜色 content[i].style.color = '#ffff'
    git学习
  • 原文地址:https://www.cnblogs.com/clc2008/p/2342716.html
Copyright © 2020-2023  润新知