zoukankan      html  css  js  c++  java
  • web项目中使用火狐浏览器导出文件时文件名乱码

    原因

    主要是编码的问题。
    在设置文件名称前,加上判断。
    判断下载者使用的浏览器,
    如果不是火狐浏览器,则对文件名称进行UTF8编码;
    如果是火狐浏览器,则不对文件名称进行操作.

    解决办法

    文件名称编码时进行判断,不是火狐浏览器时才进行编码。

    if (HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower().IndexOf("firefox") == -1) {   }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="s_path"></param>
    public static void downloadfile(string sFilePath)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(sFilePath);
        string sFileName = file.Name;
        //如果不是或火狐浏览器,则对文件名称进行UTF8编码
        if (HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower().IndexOf("firefox") == -1)
        {
            sFileName = System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8);
        }
        HttpContext.Current.Response.ContentType = "application/ms-download";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + sFileName);
        HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
        HttpContext.Current.Response.WriteFile(file.FullName);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.Clear();
        //此句注释,如果执行了此代码,则整个执行结束(不会执行下载文件方法后面的代码)
        //HttpContext.Current.Response.End();
    }
  • 相关阅读:
    Adobe Acrobat XI Pro破解版 v11.0.10中文版
    linux经典面试题
    P1540 机器翻译(STL 链表)
    P1067 多项式输出 (模拟)
    P1003 铺地毯
    [CF547C] Mike and Foam
    [CF351B] Jeff and Furik
    [CF900D] Unusual Sequences
    [CF568B] Symmetric and Transitive
    [CF893E] Counting Arrays
  • 原文地址:https://www.cnblogs.com/masonblog/p/8628637.html
Copyright © 2011-2022 走看看