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

大城市里の小女人 提交于 2019-12-11 03:09:55

问题


I have a javascript code like this :

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 :

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).
How can I solve this issue ?
Why does Request.QueryString["path"] converts all + signs to spaces ?


回答1:


When you correctly encode the query string a space becomes + and + becomes %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);


来源:https://stackoverflow.com/questions/7867603/why-does-request-querystringpath-converts-all-signs-to-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!