C# 如何对下载的文件名进行编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1598373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I encode a file name for download?
提问by eKek0
When the file name is "Algunas MARCAS que nos acompa?an" ASP.NET MVC raise an System.FormatException
when I try to download that file. But if the file name is "Asistente de Gerencia Comercial" it doesn't.
当文件名为“Algunas MARCAS que nos acompa?an”System.FormatException
时,当我尝试下载该文件时,ASP.NET MVC 会引发一个。但如果文件名是“Asistente de Gerencia Comercial”,则不是。
I guess this is because something related to UTF-8
encoding, but I don't know how to encode that string.
我想这是因为与UTF-8
编码相关的事情,但我不知道如何对该字符串进行编码。
If I'm right, how can I encode the string in UTF-8
encoding? If I'm not right, what is my problem?
如果我是对的,如何在UTF-8
编码中对字符串进行编码?如果我不对,我的问题是什么?
采纳答案by ZZ Coder
I encode file name like this for downloading,
我像这样编码文件名以供下载,
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
回答by eKek0
Based on ZZ Coder answer, and because I'm using FileResult, I decided to encode the file name as:
根据 ZZ Coder 的回答,并且因为我使用的是 FileResult,所以我决定将文件名编码为:
HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)
回答by John Fisher
I recently fought with this a bit, having many potential languages being used for the file names (Chinese is good to test with). Here is something close to what I ended up with (other implementation details excluded):
我最近对此进行了一些斗争,因为文件名使用了许多潜在的语言(中文很适合测试)。这是我最终得到的结果(不包括其他实现细节):
HttpUtility.UrlEncode("é", System.Text.Encoding.GetEncoding("ISO-8859-1"))
回答by Julian Reschke
This issue has been known for years. As far as I can tell, there currently is no interoperable way to do this, so the answer is to only support one set of browsers, or to do User Agent sniffing.
这个问题已为人所知多年。据我所知,目前没有可互操作的方法来做到这一点,所以答案是只支持一组浏览器,或者进行用户代理嗅探。
Test cases and links at: http://greenbytes.de/tech/tc2231/
测试用例和链接:http: //greenbytes.de/tech/tc2231/
回答by TJB
Also: HttpUtility considered harmful:
http://serialseb.blogspot.com/2008/03/httputilityurlencode-considered-harmful.html
另外:HttpUtility 被认为是有害的:http://serialseb.blogspot.com/2008/03/httputilityurlencode-thinked-harmful.html
I'm just going to punt and replace " " with "_" and call it a day =)
我只是打算用“_”替换“”并结束它=)
回答by ashkan
note that using UTF encoding replacesthe spaces in the file name into '+', using either the following codes produce the same results:
请注意,使用 UTF 编码会将文件名中的空格替换为“+”,使用以下任一代码会产生相同的结果:
HttpUtility.UrlEncode("é", System.Text.Encoding.GetEncoding("ISO-8859-1"))
HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)
回答by ashkan
instead of using httpUtilitythat replaces the spaces in the file name with "+" using the following code resolve the problem:
而不是使用httpUtility用“+”替换文件名中的空格,使用以下代码解决问题:
string attachment = String.Format("attachment; filename={0}",Server.UrlPathEncode(file.Name.TrimEnd()));
Response.AddHeader("Content-Disposition", attachment);
please note that if you retrieve file name from data set you may need trim the name first! you have to also add the following lines of code in advance:
请注意,如果您从数据集中检索文件名,您可能需要先修剪名称!您还必须提前添加以下代码行:
Response.Charset = "utf-8";
Response.HeaderEncoding = UnicodeEncoding.UTF8;
Response.ContentEncoding = UnicodeEncoding.UTF8;
回答by Matt Roy
The only trick that works (in all browsers) for me is:
对我来说(在所有浏览器中)唯一有效的技巧是:
Dim headerFileName As String = IIf(Request.Browser.Browser = "InternetExplorer" Or Request.UserAgent.Contains("Edge"), Uri.EscapeDataString(file.Name), file.Name)
Response.AddHeader("Content-Disposition", "attachment; filename=""" + headerFileName + """")
回答by tomRedox
We had an issue where Chrome was changing filenames that contained underscores to the name of the page the file was being downloaded from.
我们遇到了一个问题,即 Chrome 将包含下划线的文件名更改为下载文件的页面名称。
Using HttpUtility.UrlPathEncode(filename)
as suggested by Furkan Ekinci in the comments fixed it for us.
HttpUtility.UrlPathEncode(filename)
按照 Furkan Ekinci 在评论中的建议使用为我们修复了它。