简体   繁体   English

为什么Request.QueryString [“ path”]会将所有+符号转换为空格?

[英]Why does Request.QueryString[“path”] converts all + signs to spaces?

I have a javascript code like this : 我有这样的JavaScript代码:

function OnRequestComplete(result) {
        // Download the file
        //Tell browser to open file directly
        alert(result);
        var requestImage = "Handler.ashx?path=" + result;
        document.location = requestImage;
}

and Handler.ashx code is like this : 和Handler.ashx代码是这样的:

public void ProcessRequest(HttpContext context)
{
    Context = context;
    string filePath = context.Request.QueryString["path"];
    filePath = context.Server.MapPath(filePath);
}   

In filePath we don't have any + signs (spaces instead). 在filePath中,我们没有任何+号(而是空格)。
How can I solve this issue ? 我该如何解决这个问题?
Why does Request.QueryString["path"] converts all + signs to spaces ? 为什么Request.QueryString [“ path”]会将所有+符号转换为空格?

When you correctly encode the query string a space becomes + and + becomes %2B . 当您正确编码查询字符串时,空格变为++变为%2B The process of decoding does the reverse, which is why your + gets turned into a space. 解码的过程相反,这就是为什么+变成空格。

The problem is that you didn't encode the query string, and that means it gets decoded incorrectly. 问题是您没有查询字符串进行编码 ,这意味着它被错误地解码。

var requestImage = "Handler.ashx?path=" + encodeURIComponent(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM