正确设置Firefox下载文件文件名的方法

不同的浏览器需要特殊设置,主要是火狐比较特殊,火狐可能给文件名加上“%0d%0a"这样的编码字符(换行的意思)。不得不佩服网上的高手,这也能解决。

 1 [HttpGet]
 2 public FileResult Download(string id)
 3 {
 4      var document = service.GetDocument(id);
 5      var fullName = Path.Combine(Root, document.FullName);
 6      string browser = HttpContext.Request.UserAgent.ToUpper();
 7      var fileName = document.Name;
 8      if (browser.Contains("FIREFOX"))
 9           fileName = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(fileName)) + "?=";
10      else
11           fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
12      return File(fullName, document.ContentType, fileName);
13 }

对于火狐的处理(标红的代码),我也是醉了,根本没看懂,但问题解决了!

【参考文献】http://www.cnblogs.com/godtrue/p/4333262.html

原文地址:https://www.cnblogs.com/tuty/p/6002493.html