简体   繁体   中英

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

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 ?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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