为什么HttpUtility.UrlEncode(HttpUtility.UrlDecode(“%20”))返回+而不是%20? [英] Why does HttpUtility.UrlEncode(HttpUtility.UrlDecode("%20")) return + instead of %20?
问题描述
基本上我在这里遇到问题:
我有一个文件下载的问题,下载正在替换所有空格的下划线。 p>
Response.AddHeader(Content-Disposition,
attachment; filename =+ someFileName);
问题是如果someFileName中有一个空格,例如check this out.txt,那么将提示用户下载check_this_out.txt。
我认为最好的选择是UrlEncode文件名,所以我试过
HttpUtility.UrlEncode(someFileName);
但是它正在用加号替换空格,这让我失望了。那么我刚刚尝试过
HttpUtility.UrlEncode(HttpUtility.UrlDecode(%20))
,解码工作正常,给我一个空间,但是编码占用空间,然后再次给我加号。 p>
我在这里缺少什么,这是正确的吗?如果是这样,我应该如何正确地将空格编码为%20,这是我需要的。
从此链接
我自己遇到了这个。如果
可以将空格更改为%20s
,那么IE7将正确转换它们。
Firefox虽然会以
的字面值(至少在使用
Content-disposition头文件时)使用,但是
只需要
IE7才能执行此操作。
我们在我们的应用程序中执行了以下操作(a
tomcat的文档存储库)String userAgent = request.getHeader(User-Agent);
if(userAgent.contains(MSIE 7.0)){
filename = filename.replace(,%20);
}
response.addHeader(Content-disposition,
attachment; filename = \+ filename +\);
I'm having a problem with a file download where the download is replacing all the spaces with underscores.
Basically I'm getting a problem here:
Response.AddHeader("Content-Disposition",
"attachment; filename=" + someFileName);
The problem is that if someFileName had a space in it such as "check this out.txt" then the user would be prompted to download "check_this_out.txt".
I figured the best option would be to UrlEncode the filename so I tried
HttpUtility.UrlEncode(someFileName);
But it's replacing the spaces with plus signs, which stumped me. So then I just tried
HttpUtility.UrlEncode(HttpUtility.UrlDecode("%20"))
and the decode works properly and gives me a space, but the encode takes the space and then gives me the plus sign again.
What am I missing here, is this correct? If so, how should I properly encode spaces into %20's, which is what I need.
Quoting from this link
I've come across this myself. If you are able to change the spaces to %20s then IE7 will convert them correctly. Firefox though will take them literally ( at least when using the Content-disposition header) so you will need to do this for requests from IE7 only.
We did the following in our app. ( a tomcat based document repository)
String userAgent = request.getHeader("User-Agent"); if (userAgent.contains("MSIE 7.0")) { filename = filename.replace(" ", "%20"); } response.addHeader("Content-disposition", "attachment;filename=\"" + filename + "\"");
这篇关于为什么HttpUtility.UrlEncode(HttpUtility.UrlDecode(“%20”))返回+而不是%20?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!