C# 为什么Request.QueryString[“path”转换所有+;空间标志?
我有如下javascript代码:C# 为什么Request.QueryString[“path”转换所有+;空间标志?,c#,asp.net,request,query-string,sign,C#,Asp.net,Request,Query String,Sign,我有如下javascript代码: function OnRequestComplete(result) { // Download the file //Tell browser to open file directly alert(result); var requestImage = "Handler.ashx?path=" + result; document.location = requestImage
function OnRequestComplete(result) {
// Download the file
//Tell browser to open file directly
alert(result);
var requestImage = "Handler.ashx?path=" + result;
document.location = requestImage;
}
Handler.ashx代码如下:
public void ProcessRequest(HttpContext context)
{
Context = context;
string filePath = context.Request.QueryString["path"];
filePath = context.Server.MapPath(filePath);
}
在文件路径中,我们没有任何+符号(而是空格)。如何解决此问题?
为什么Request.QueryString[“path”]会将所有+符号转换为空格 正确编码查询字符串时,空格变为
+
,+
变为%2B
。解码的过程正好相反,这就是为什么你的+
会变成一个空格的原因
问题是您没有输入查询字符串,这意味着它被错误地解码
var requestImage = "Handler.ashx?path=" + encodeURIComponent(result);
谢谢你的回答/我应该用javascript编码这个查询字符串吗?怎么做?检查这个答案:querystring有自己的语法和保留字符。编码你自己的文件名。