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();
            }


  • 相关阅读:
    【C++】深度探索C++对象模型读书笔记--关于对象(Object Lessons)
    【操作系统】调度算法
    【vim】vim常用命令
    【linux】linux的数据流重定向
    以太网帧,IP,TCP,UDP首部结构
    IPv4编址及子网划分
    【计算机网络】计算机网络模型
    【计算机网络】NAT:网络地址转换
    【设计模式】C++中的单例模式
    (转)linux查找技巧: find grep xargs
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/858741.html
Copyright © 2011-2022 走看看