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();
    
            }
  • 相关阅读:
    vue中富文本编辑框
    vue中生成二维码
    在ABP中使用linq
    js根据年月得到当前这个月总共有多少天
    mescroll在vue中的应用
    javascript积累
    javascript常用的操作
    情侣间常犯的7个沟通问题
    欧洲旅游六大最佳目的地
    见与不见
  • 原文地址:https://www.cnblogs.com/shiliang199508/p/6763338.html
Copyright © 2011-2022 走看看