• 解决下载文件名乱码问题的简单方法


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


  • 相关阅读:
    chmod命令详细用法
    mysql删除sql表添加别名及删除sql的注意事项
    bootstrap栅格系统进行偏移格式
    mysql中时间计算函数SQL DATE_SUB()用法
    阿里图标的应用教程
    jquery.cookie.js中$.cookie() 使用方法
    $.cookie()取值设置
    java中年月日的加减法,年月的加减法使用
    IMAP命令与分析
    Telnet IMAP Commands Note
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/858741.html
Copyright © 2020-2023  润新知