VWG网页下载时中文乱码的解决方法

网页下载时,弹出的保存窗体中,文件名中的汉字显示的是乱码!
解决此问题:
如下代码中,将priFileName,使用HttpUtility.UrlPathEncode进行编码即可。
Link.Download(new GatewayResourceHandle(this, GetPathID(priFilePath)), HttpUtility.UrlPathEncode(priFileName));

参考资料:

HttpUtility 类
          .NET Framework 2.0
提供用于在处理 Web 请求时编码和解码 URL 的方法。无法继承此类。

HttpUtility 类由 HttpServerUtility 类在内部使用,后者的方法和属性通过内部 ASP.NET Server 对象公开。此外,HttpUtility 类包含一些不能从 Server 访问的编码和解码实用工具方法。

<%@ Page Language="C#"%>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
String currurl = HttpContext.Current.Request.RawUrl;
String querystring = null ;

// Check to make sure some query string variables
// exist and if not add some and redirect.
int iqs = currurl.IndexOf('?');
if (iqs == -1)
{
String redirecturl = currurl + "?var1=1&var2=2+2%2f3&var1=3";
Response.Redirect(redirecturl, true);
}
// If query string variables exist, put them in
// a string.
else if (iqs >= 0)
{
querystring = (iqs < currurl.Length - 1) ? currurl.Substring(iqs + 1) : String.Empty;
}

// Parse the query string variables into a NameValueCollection.
NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

// Iterate through the collection.
StringBuilder sb = new StringBuilder();
foreach (String s in qscoll.AllKeys)
{
sb.Append(s + " - " + qscoll[s] + "<br>");
}

// Write the results to the appropriate labels.
ParseOutput.Text = sb.ToString();
UrlRawOutput.Text = currurl;
UrlEncodedOutput.Text = HttpUtility.UrlEncode(currurl);
UrlDecodedOutput.Text = HttpUtility.UrlDecode(currurl);
}
</script>

<html>
<head runat="server">
<title>HttpUtility ParseQueryString Example</title>
</head>
<body>
<form runat="server">
The raw url is: <br />
<asp:Label id="UrlRawOutput"
runat="server" />
<br /><br />
The url encoded is: <br />
<asp:Label id="UrlEncodedOutput"
runat="server" />
<br /><br />
The url decoded is: <br />
<asp:Label id="UrlDecodedOutput"
runat="server" />
<br /><br />
The query string NameValueCollection is: <br />
<asp:Label id="ParseOutput"
runat="server" />
</form>
</body>
</html>



posted on 2012-02-07 22:31  kingang  阅读(384)  评论(0编辑  收藏  举报

导航