SilverLight
SilverLight

Reputation: 20488

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 ?

Upvotes: 2

Views: 2243

Answers (1)

Mark Byers
Mark Byers

Reputation: 839234

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

Upvotes: 4

Related Questions