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