Responsible for the website in ASP.NET pages with a program for a download Word / Excel files, recent user requests a file name contains Traditional Chinese characters,
colleagues normal display with chrome / firefox browser download test, but with IE (version 11) is turned on, there was garbled.
1. Prepare a file Chinese file name
ASP.NET MVC2. Add in HomeControler the download Download
public ActionResult Download()
{
//文件位置
string filepath = @"E:testWebApplication1中文文件名.docx";
//文件名称
string filename = Path.GetFileName(filepath);
//读成串流
Stream sfilestream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
//回传出文件
return File(sfilestream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename);
}
3. Perform download
Filename normal display Chinese!
Put on ASP.NET Web Form
4. The first increase in a Download.aspx
In Page load join download
protected void Page_Load(object sender, EventArgs e)
{
string filepath = @"E:testWebApplication1中文文件名.docx";
string fileName = Path.GetFileName(filepath);
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
HttpContext.Current.Response.WriteFile(filepath);
HttpContext.Current.Response.End();
}
5. Perform download
File name is not normal
Found two solution
- The Chinese file name HttpUtility.UrlEncode to UTF8 encodingQP coding system.
- HeaderEncoding coding instead BIG5。
(1) The Chinese filenames QP encoding to UTF8 encoding system with HttpUtility.UrlEncode
string fileName = HttpUtility.UrlEncode(Path.GetFileName(filepath));
//string fileName = Path.GetFileName(filepath);
HttpUtility.UrlEncode for default QP (Quoted-Printable) UTF8 encoding the encoding system, 7 Bit character (ASCII) can be displayed directly on without special conversion.
- Coding ago:inCulturefilesname.docx
- After the code:% E4% b8% ad% E6% 96% 87% E6% aa% 94%e5% 90% 8d.docx
In addition to the standard ASCII, the other will be encoded by the browser to the distal UTF8.
https://mothereff.in/utf-8
The file name can show the normal Chinese!
(2) HeaderEncoding encoded to BIG5
Add a line to set HeaderEncoding program, because servers are Traditional Chinese, default Encoding.Default = BIG5
HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.HeaderEncoding default is UTF8, BIG5 encoding can be changed to solve the problem of Chinese file name
都已经是UTF8编码的网页了,为什么还得要将HeaderEncoding编码改为繁体中文操作系统的BIG5才能解决???
进一步搜寻到保哥、黑暗大文章,看起来似乎与浏览器支持到哪一种Content-Disposition标准有关(RFC2045、2183、5987)。
好,先到此打住,来看奥运了!以后有时间再继续深入调查。
小结:
- 因为系统被要求要支持多国语系,暂时我们选择第一种解法,以后出现蝌蚪文还是火星文就不怕了。
- ASP.NET MVC已经解决了文件名有中文码问题了。
参考:
HttpResponse 类
HttpUtility.UrlEncode
HttpResponse.HeaderEncoding
KB-Open And Download File In Chinese Filename
ASP.NET 如何设定强制下载文件并正确处理中文文件名的问题
How to encode the filename parameter of Content-Disposition header in HTTP?
原文:大专栏 [ASP.NET][C#]下载文件时中文文件名出现乱码