When a string is read from the database, the space in the string becomes the plus sign.

Source: Internet
Author: User
HttpUtility. urlEncode converts null to the plus sign () in Encode, and converts the plus sign to null in Decode. However, the browser cannot understand that the plus sign is empty, so if the file name contains null, if the file downloaded from the browser is null, it becomes the plus sign. When developing pages using ASP. Net, we often use System. Web

HttpUtility. when UrlEncode is in Encode, it is converted to the plus sign (''). When Decode is used, the plus sign is converted to null, but the browser cannot understand that the plus sign is empty, therefore, if the file name contains null characters, the file downloaded from the browser becomes the plus sign. When developing pages using ASP. Net, we often use System. Web

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.

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.

At this time, the problem arises, 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 ");

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.