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

    在a标签href属性直接写文件地址有些文件不会进入下载(例如 图片类型),浏览器会自动打开预览
    这时可以使用下面这种方式进行文件下载

     Html代码

    <a href="/DownloadFile?fileName=fileName&path=path">点击下载</a>

    C#代码

            /// <summary>
            /// 文件下载
            /// </summary>
            /// <param name="fileName">文件名(下载后的名字)</param>
            /// <param name="path">文件路径</param>
            public void DownloadFile(string fileName, string path)
            {
                HttpContext.Response.ContentType = "application/ms-download";
    
                string s_path = path;
                WebClient wc = new WebClient();
                wc.Encoding = Encoding.UTF8;
                byte[] temp_byte = wc.DownloadData(s_path);
    
                HttpContext.Response.Clear();
                HttpContext.Response.AddHeader("Content-Type", "application/octet-stream");
                HttpContext.Response.Charset = "utf-8";
                HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                HttpContext.Response.AddHeader("Content-Length", temp_byte.Length.ToString());
                HttpContext.Response.BinaryWrite(temp_byte);
                HttpContext.Response.Flush();
                HttpContext.Response.Clear();
                HttpContext.Response.End();
    
            }
  • 相关阅读:
    CF
    求最长反链 || Dilworth 定理
    APIO 2020 补题记录
    CF vp 记录
    虚树
    LCT 学习
    平衡树
    poly
    关于此博客
    题解 P5021【NOIP2018】 【赛道修建】
  • 原文地址:https://www.cnblogs.com/shiliang199508/p/6763338.html
Copyright © 2011-2022 走看看