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();
    
            }
  • 相关阅读:
    基本语句
    mysql多表查询方法(join)
    MySQL JOIN 多表连接
    MySQL SHOW INDEX语法的实际应用
    1.索引作用
    MySQL索引和优化查询
    mysql复合索引、普通索引总结
    mysql 索引相关
    for循环的break和continue
    保护程序猿滴眼睛---修改VS 2012 编辑器颜色
  • 原文地址:https://www.cnblogs.com/shiliang199508/p/6763338.html
Copyright © 2011-2022 走看看