Server. urlencode, httputility. urldecode different encoding when downloading the file name becomes garbled

Source: Internet
Author: User
The same server. urlencode has different encoded characters on different pages. Later, I checked the original characters .. Which one should I use for URL encoding? Are there any differences between these two methods?
Test:
String file = "file (upload document .doc ";
String server_urlencode = server. urlencode (File );
String server_urldecode = server. urldecode (server_urlencode );
String httputility_urlencode = system. Web. httputility. urlencode (File );
String httputility_urldecode = system. Web. httputility. urldecode (httputility_urlencode );
Response. Write ("original data:" + file );
Sfun. writeline ("server. urlencode:" + server_urlencode );
Sfun. writeline ("server. urldecode:" + server_urldecode );
Sfun. writeline ("httputility. urlencode:" + httputility_urlencode );
Sfun. writeline ("httputility. urldecode:" + httputility_urldecode );

Output:
Original data: file (upload document .doc
Server. urlencode: performance%c41_bc1_few.c91_cf1_a3%a8%b4% AB %a3%a9%c6%aa.doc
Server. urldecode: file (upload example .doc
Httputility. urlencode: Invalid
Httputility. urldecode: file (upload example .doc

The difference is that httputility. urlencode () is UTF-8 encoded by default, while server. urlencode () is encoded by default.

ASP. when developing pages, we often use system. web. httputility. urlencode and urldecode PASS Parameters between pages through URLs. the use of encode and decode in pairs is correct.

However, when writing a file download page, we often use the following method to specify the name of the downloaded file:
Response. addheader ("content-disposition", "attachment; filename ="
+ Httputility. urlencode (filename, encoding. utf8 ));
The reason for conversion to utf8 is to support Chinese file names.

This is the case because httputility. urlencode converts a space into a plus sign ('+') in encode, and converts the plus sign to a space in Decode. However, the browser cannot understand the plus sign as a space, therefore, if the file name contains spaces, the space of the file downloaded from the browser becomes the plus sign.

One solution is to replace "+" with "% 20" after the urlencode of httputility (if it is "+", it is converted to "% 2B"), for example:
Filename = httputility. urlencode (filename, encoding. utf8 );
Filename = filename. Replace ("+", "% 20 ");
I don't understand why Microsoft converts spaces to plus signs instead of "% 20". Remember that JDK urlencoder converts spaces to "% 20.
After checking, the same is true in. NET 2.0.



The above is copied from other places and is well written. One of my own Program Also encountered the same problem, the default aspx is UTF-8 encoded, in my program, gb2312 must be used as the default encoding (<globalization requestencoding = "gb2312" responseencoding = "gb2312"/>). The problem occurs and there is no problem with httputility. urldecode in page. the value returned by the request is garbled. This is the httputility mentioned above. by default, urldecode uses utf8 to encode the URL. In this case, you only need to encode httputility. change urldecode to server. urlencode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.