日期:2014-05-18  浏览次数:20845 次

动态推送文件名,在IE下没有问题,Opera下有乱码。
一段很简单的代码:

  string fileName = "opera是大傻瓜.doc";
  byte[] fileContent = System.Text.Encoding.UTF8.GetBytes(fileName);
  HttpContext.Current.Response.Buffer = true;
  HttpContext.Current.Response.ClearContent();
  HttpContext.Current.Response.ClearHeaders();
  HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8 ;

  HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.BinaryWrite(fileContent);
  HttpContext.Current.Response.Flush();
  HttpContext.Current.Response.Close();
  HttpContext.Current.Response.Clear();

在IE下运行的时候,会出现下载对话框,其中,文件名是中文正常的。但是在Opera下面,文件名变成了类似被URL编码的文件名:opera%e6%98%af%e5%a4%a7%e5%82%bb%e7%93%9c.doc。

------解决方案--------------------
帮LZ顶
------解决方案--------------------
判断一下是不是非IE


string fileName = "opera是大傻瓜.doc"; 
if(Request.Browser.Type.IndexOf("IE") != -1)
{
fileName = System.Web.HttpUtility.UrlEncode(fileName);
}
byte[] bf = System.Text.Encoding.GetEncoding("GB2312").GetBytes(fileName);
byte[] fileContent = System.Text.Encoding.UTF8.GetBytes(fileName); 
HttpContext.Current.Response.Buffer = true; 
HttpContext.Current.Response.ClearContent(); 
HttpContext.Current.Response.ClearHeaders(); 
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); 

HttpContext.Current.Response.ContentType = "application/octet-stream"; 
HttpContext.Current.Response.BinaryWrite(fileContent); 
HttpContext.Current.Response.Flush(); 
HttpContext.Current.Response.Close(); 
HttpContext.Current.Response.Clear();