zoukankan      html  css  js  c++  java
  • 解决下载文件名乱码问题的简单方法

    (1)方法一:
    string fileName="中文.xls";

    string filePath = @"/UpLoad/Reports"

    FileInfo file = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath)+fileName);
       Response.Charset = "utf-8";
       Response.ContentEncoding = System.Text.Encoding.UTF8;

       // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
       Response.AddHeader("Content-Disposition", "attachment; filename=" +HttpUtility.UrlEncode("下载文件"+".xls",System.Text.Encoding.UTF8));
       // 添加头信息,指定文件大小,让浏览器能够显示下载进度
       Response.AddHeader("Content-Length", file.Length.ToString());
       // 指定返回的是一个不能被客户端读取的流,必须被下载
       Response.ContentType = "application/ms-excel";
       // 把文件流发送到客户端
       Response.WriteFile(file.FullName);
       // 停止页面的执行
       Response.End();  

    (2)方法二:
    /// <summary>
            /// 下载文件
            /// </summary>
            /// <param name="path">要下载的文件的路径</param>
            /// <param name="name">下载文件的保存名称</param>
            public static void downLoad(string path,string name)
            {
                FileStream fileStream = new FileStream(path, FileMode.Open);
                long fileSize = fileStream.Length;
                byte[] fileBuffer = new byte[fileSize];
                fileStream.Read(fileBuffer, 0, (int)fileSize);
                System.Web.HttpContext.Current.Response.ClearHeaders();


                System.Web.HttpContext.Current.Response.Charset = "utf-8";
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

                HttpContext.Current.Response.ContentType = "application/octet-stream";
                System.Web.HttpContext.Current.Response.AppendHeader("Content-Type", "application/octet-stream");// "application/octet-stream");
                System.Web.HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
                System.Web.HttpContext.Current.Response.BinaryWrite(fileBuffer);
                HttpContext.Current.Response.End();
            }


  • 相关阅读:
    统计字符串中每个字母出现的次数
    三次握手和四次挥手
    select后面不能包含group by 后面没有的列
    常用adb命令
    replace和replaceAll的区别
    java统计一个字符串中某个字串出现的次数
    大厂如何解决分布式事务
    ADB 用法大全
    PBN飞越转弯Flyover衔接TF、CF航段保护区组图
    PBN旁切转弯保护区组图
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/858741.html
Copyright © 2011-2022 走看看