zoukankan      html  css  js  c++  java
  • .net MVC 下载文件乱码问题解决方案

    1. public ActionResult OverAllSummaryExport(string id)  
    2. {  
    3.     #region 解决中文乱码  
    4.     Response.HeaderEncoding = Encoding.UTF8;  
    5.     string fileName = "安全生产标准化自评结果整体输出.doc";  
    6.     if (Request.UserAgent != null)  
    7.     {  
    8.         string userAgent = Request.UserAgent.ToUpper();  
    9.         if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)  
    10.             fileName = ToUtf8String(fileName);  
    11.     }  
    12.     #endregion  
    13.     string HTMLStr =string.Format("<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8">{0}</head><body>{1}</body></html>",GetStyle(),GetBody(id));  
    14.     return File(Encoding.UTF8.GetBytes(HTMLStr), "application/vnd.ms-word", fileName);  
    15. }  


      1. /// <summary>   
      2. /// 解决下载名称在IE下中文乱码   
      3. /// </summary>   
      4. /// <param name="s"></param>   
      5. /// <returns></returns>   
      6. private String ToUtf8String(String s)  
      7. {  
      8.     StringBuilder sb = new StringBuilder();  
      9.     for (int i = 0; i < s.Length; i++)  
      10.     {  
      11.         char c = s[i];  
      12.         if (c >= 0 && c <= 255)  
      13.         {  
      14.             sb.Append(c);  
      15.         }  
      16.         else  
      17.         {  
      18.             byte[] b;  
      19.             try  
      20.             {  
      21.                 b = Encoding.UTF8.GetBytes(c.ToString());  
      22.             }  
      23.             catch (Exception ex)  
      24.             {  
      25.                 b = new byte[0];  
      26.             }  
      27.             for (int j = 0; j < b.Length; j++)  
      28.             {  
      29.                 int k = b[j];  
      30.                 if (k < 0) k += 256;  
      31.   
      32.                 sb.Append("%" + Convert.ToString(k, 16).ToUpper());  
      33.             }  
      34.         }  
      35.     }  
      36.     return sb.ToString();  

  • 相关阅读:
    使用TortoiseGit从GitHub下拉上传代码配置
    Git 安装和使用教程(转载)
    C++的STL之map自动排序特性
    C语言实现随机生成0~100的数
    C语言实现随机生成0或1
    和 区别
    C语言文件操作函数
    php的缓冲/缓存 js对象 ,php编程的深入思考-1
    apache安装时的一些术语
    在linux下手动安装 apache, php, mysql--终极版
  • 原文地址:https://www.cnblogs.com/zxktxj/p/3635605.html
Copyright © 2011-2022 走看看