zoukankan      html  css  js  c++  java
  • 在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法

            [CheckSession]
            public ActionResult ExpDataTwo()
            {
                try
                {         
                    string Status_Missionn = Request.QueryString["sexch"];
                    ParadoxWebService.ParadoxServiceClient client = new ParadoxWebService.ParadoxServiceClient();
                    ExportResultInfo res = client.ExportParadoxDataBase(LoadExportSqlStrTwo(Status_Missionn), "GLJ");
                    string XMlDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Download/");
                    string fileName = Session["hangji"].ToString() + Session["atmTwo_Code"].ToString() + DateTime.Now.ToString("yyyyMMddmmss") + ".DB";   // 以分秒为文件名。

                    # region 处理乱码
                    Encoding encoding;
                    string outputFileName = null;
                    string browser = Request.UserAgent.ToUpper();
                    if (browser.Contains("MS") == true && browser.Contains("IE") == true)
                    {
                        outputFileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", "%20");
                        encoding = System.Text.Encoding.Default;
                    }
                    else if (browser.Contains("FIREFOX") == true)
                    {
                        outputFileName = fileName;
                        encoding = System.Text.Encoding.GetEncoding("GB2312");
                    }
                    else
                    {
                        outputFileName = HttpUtility.UrlEncode(fileName);
                        encoding = System.Text.Encoding.Default;
                    }
                    #endregion

                    string url = XMlDirectory + fileName;
                    ConvertToDB(res.ExportDbBuffer, url);
                    return File(url, "application/vnd.borland", outputFileName);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

    /// <summary>
            /// 字节转化为文件。
            /// </summary>
            /// <param name="buffer"></param>
            private void ConvertToDB(byte[] buffer, string filePath)
            {
                FileStream fstream = System.IO.File.Create(filePath, buffer.Length);
                try
                {
                    fstream.Write(buffer, 0, buffer.Length);
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    fstream.Close();
                }
            }

  • 相关阅读:
    iis7无法写入配置文件
    重写基类方法与隐藏基类方法的区别
    观察者模式上班玩游戏,老总是怎么知道的?
    Session过期和清除缓存 .
    C#基础概念
    php反序列化漏洞绕过魔术方法 __wakeup
    Shadow broker=>fuzzbunch+metasploit 攻击外网测试以及metasploit大批量扫描目标IP
    Python “ValueError: incomplete format” upon print(“stuff %” % “thingy”) 解决方法
    CSTC2017Webwriteup
    php 弱类型总结
  • 原文地址:https://www.cnblogs.com/zh1989/p/4241124.html
Copyright © 2011-2022 走看看