asp.net upload or download when the filename contains a special character "#" for handling _ practical Tips

Source: Internet
Author: User
Tags number sign
such as code similar to the following:
Copy Code code as follows:

Uri uri = new Uri (targeturl);//targeturl absolute path of corresponding file
System.Net.HttpWebRequest request = (HttpWebRequest) webrequest.create (URI);
Request. method = ' Put ';
Request. Credentials = System.Net.CredentialCache.DefaultCredentials;
Request. ContentLength = stream. Length;

File name: Fr#32%.xls (Excel Attachment)
Uploaded to the server, found to become fr.xls, the filename is not right, so the result is not download.
The reason is that some URIs include segment identifiers or queries. The segment identifier is any text that is followed by a number sign (#) in the URI and is stored in the Fragment property.
The query information is any text that is followed by a question mark (?) in the URI and is stored in the query property. That is, the URI class splits the contents behind the file path # number.
Also, the related attributes in the URI are read-only (ReadOnly), and can only be modified by other paths.
Workaround:
UriBuilder class, provides a custom constructor for the Uniform Resource Identifier (URI), and modifies the URI of the URI class. Like the function of a URI, but its associated properties can be set.
The modified code is as follows:
Copy Code code as follows:

Uri uri = webhelper.processspecialcharacters (targeturl);//targeturl the absolute path of the corresponding file
System.Net.HttpWebRequest request = (HttpWebRequest) webrequest.create (URI);
Request. method = ' Put ';
Request. Credentials = System.Net.CredentialCache.DefaultCredentials;
Request. ContentLength = stream. Length;
<summary>
When the file name that is uploaded or downloaded contains a special character "#", you need to perform the following functions for processing
</summary>
<param name= "Url" ></param>
<returns></returns>
private static Uri Processspecialcharacters (String Url)
{
Uri uritarget = new Uri (URL);
if (! Url.contains ("#"))
{
return uritarget;
}
UriBuilder mspage = new UriBuilder ();
Mspage.host = Uritarget.host;
Mspage.scheme = Uritarget.scheme;
Mspage.port = Uritarget.port;
Mspage.path = Uritarget.localpath + uritarget.fragment;
Mspage.fragment = uritarget.fragment;
Uri uri = Mspage.uri;
return URI;
}
Uri uri = new Uri (targeturl);//targeturl absolute path of corresponding file
Related Article

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.