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

    /// <summary>
            /// 下载文件
            /// </summary>
            /// <param name="FullFileName"></param>
            public static void FileDownload(string FullFileName)
            {
                FileInfo DownloadFile = new FileInfo(FullFileName); //设置要下载的文件
              
                 HttpContext.Current.Response.Clear(); //清除缓冲区流中的所有内容输出
                 HttpContext.Current.Response.ClearHeaders(); //清除缓冲区流中的所有头
                 HttpContext.Current.Response.Buffer = false; //设置缓冲输出为false
                //设置输出流的 HTTP MIME 类型为application/octet-stream
                 HttpContext.Current.Response.ContentType = "application/octet-stream";
                //将 HTTP 头添加到输出流
                string s = HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(DownloadFile.Name));
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + s);// HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
    
                HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
    
                //将指定的文件直接写入 HTTP 内容输出流。
    
                HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
                HttpContext.Current.Response.Flush(); //向客户端发送当前所有缓冲的输出
                HttpContext.Current.Response.End(); //将当前所有缓冲的输出发送到客户端
    
            }

    调用代码:

    string path = "/UploadFile/Template/ImportApplyInfo.xlsx";//目录为当前web下目录
    FileDownload(this.Server.MapPath(path));
  • 相关阅读:
    左右对齐Justify遇到的坑
    JS中的相等性判断===, ==, Object.is()
    JS调用栈的一些总结
    VueI18n
    【转】Webpack 快速上手(下)
    【转】Webpack 快速上手(中)
    【转】Webpack 快速上手(上)
    springboot打包排除指定jar包依赖
    prometheus+grafana搭建
    fbctf 安装部署出现的问题
  • 原文地址:https://www.cnblogs.com/buzi521/p/3875040.html
Copyright © 2011-2022 走看看