简体   繁体   English

为什么HttpUtility.UrlEncode(HttpUtility.UrlDecode(“%20”))返回+而不是%20?

[英]Why does HttpUtility.UrlEncode(HttpUtility.UrlDecode(“%20”)) return + instead of %20?

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". 问题是如果someFileName中有一个空格,例如“check this out.txt”,则会提示用户下载“check_this_out.txt”。

I figured the best option would be to UrlEncode the filename so I tried 我认为最好的选择是UrlEncode文件名,所以我试过

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. 如果是这样,我应该如何正确地将空格编码为%20,这就是我需要的。

Basically both %20 and + are valid ways of encoding a space. 基本上,%20和+都是编码空间的有效方法。 Obviously the UrlEncode method has to pick one of the options... if it chose to do the other way, someone else would have asked why UrlEncode(UrlDecode("+")) returned "%20"... 显然,UrlEncode方法必须选择其中一个选项...如果它选择以其他方式执行,其他人会问为什么UrlEncode(UrlDecode("+"))返回“%20”...

You could always encode it, then just do a straight string replace on "+" for "%20". 您可以随时对其进行编码,然后在“+”上为“%20”执行直接字符串替换。 I think that would work... 认为这会奏效......

I figured the best option would be to UrlEncode the filename 我认为最好的选择是UrlEncode文件名

That's not the right way to put out-of-band characters in a header parameter such as Content-Disposition-filename, and only works (sometimes) in IE due to a bug. 这不是将带外字符放在诸如Content-Disposition-filename之类的头参数中的正确方法,并且由于错误而仅在IE中工作(有时)。 Actually it's a bit of a perennial problem: there is no right way . 实际上这是一个长期存在的问题: 没有正确的方法

If you need to put special characters in the downloaded filename, you can't do it reliably with Content-Disposition-filename. 如果您需要在下载的文件名中放入特殊字符,则无法使用Content-Disposition-filename可靠地执行此操作。 Instead, omit the 'filename' parameter from the Content-Disposition-attachment header, and leave the filename you want in the trailing part of the URL. 相反,省略Content-Disposition-attachment标头中的'filename'参数,并将所需的文件名保留在URL的尾部。 In the absence of a filename parameter the browser will take it from the URL path, where URL-encoding is the right way to tackle special characters. 如果没有文件名参数,浏览器将从URL路径中获取它,其中URL编码处理特殊字符的正确方法。

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. 如果您能够将空格更改为%20s,那么IE7将正确转换它们。 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. 虽然Firefox会逐字逐句地使用它们(至少在使用Content-disposition标头时),因此您只需要对来自IE7的请求执行此操作。

We did the following in our app. 我们在应用程序中执行了以下操作。 ( a tomcat based document repository) (基于tomcat的文档存储库)

 String userAgent = request.getHeader("User-Agent"); if (userAgent.contains("MSIE 7.0")) { filename = filename.replace(" ", "%20"); } response.addHeader("Content-disposition", "attachment;filename=\\"" + filename + "\\""); 

Hi I also faced same kind of problem when downloading the files having spaces in them. 嗨我在下载包含空格的文件时也遇到了同样的问题。

Please see the link which best suites and gives the complete answer. 请查看最佳套房的链接并给出完整的答案。

http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download

For the sake of understanding I am just adding the ASP.net code how to solve this problem. 为了便于理解我只是添加ASP.net代码如何解决这个问题。

string document = @"C:\Documents and Settings\Gopal.Ampolu\My Documents\Downloads\" + "Disciplinary & Grievance Procedures..doc";
string filename = "Disciplinary & Grievance Procedures..doc";

Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", @"attachment; filename=""" + HttpUtility.UrlDecode(filename) + @"""");
Response.Flush();

From the above you can see that while adding header to the response, filename is enclosed with double quotes. 从上面你可以看到,在向响应添加标题时,filename用双引号括起来。 Please make sure that the "filename" must be Decoded with UrlDecode. 请确保必须使用UrlDecode解码“文件名”。

One more option is also there, if you can intimate clients about windows hotfix update available at following: 还有一个选项,如果您可以通过以下方式获得关于Windows修补程序更新的亲密客户:

Windows Hotfix Update for IE white space issue 用于IE空白问题的Windows修补程序更新

It is client side, so may not be applicable to all the scenarios but still an option if feasible. 它是客户端,因此可能不适用于所有场景,但如果可行,仍然是一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM