ibcadmin 发表于 2015-9-6 09:26:16

ASP.NET下载中文文件名乱码解决方法

转载,代码未经楼主测试

try
      {
            string excelName = Request["excelName"].ToString(); //文件路径
            string fileName = Request["fileName"].ToString(); //下载的文件名
            if(!(string.IsNullOrEmpty(excelName)|| string.IsNullOrEmpty(fileName)))
            {
                Response.Charset = "GB2312";
                Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                Response.ContentType="application/vnd.ms-excel";
                //火狐浏览器不需将中文文件名进行编码格式转换
                if (Request.ServerVariables["http_user_agent"].ToLower().IndexOf("firefox") == -1)
                {
                  excelName = HttpUtility.UrlEncode(excelName, System.Text.Encoding.UTF8);
                }
                Response.AppendHeader("Content-Disposition", "attachment;filename=" +excelName);
                Response.WriteFile(fileName);
                Response.Flush();
                Response.Close();
            }
      }
      catch
      { }

推人落水 发表于 2015-10-27 13:08:44

頂一個
页: [1]
查看完整版本: ASP.NET下载中文文件名乱码解决方法