URLEncode

R

Russ

Response.Redirect(@".\folder\" + filename);

How could I urlencode this statement so that if the filename contains a
special character (in my case a #) the redirect will still function?
 
M

mikeb

Russ said:
Here is what I came up with

Response.Redirect(@".\folder\" + Server.UrlEncode(filename));

Now if my filename is 5373453 Store #2345.pdf

This is what I get in the address bar:
http://localhost/Path/folder/5373453+Store+#2345.pdf
This is not working though.

Anyone know why it is not working?

Possibly. Server.UrlEncode() encodes things using a pseudo-standard based
on how browsers implemented the <isindex> tags a long time ago, and how
query parameters are usually encoded even today. Spaces get encoded to '+'.
This type of encoding is only suitable for the query parameters portion of a
URL.

RFC 1738 specifies that spaces should be encoded using %20. As far as I
know, there's no easily accessible method to RFC 1738 encode URLs in the
framework:

- if using JScript.NET you can call the encodeURI() or the
encodeURIComponent() global function

- there are protected methods of the Uri class that do this, if you
want to create a class that inherits from it to do the work you need

- or you can use his hack:

string encodedUrl = (new UriBuilder( "http", "localhost", 80,
originalFilename)).Path;


However, I don't think the UriBuilder constructor will encode the '#'
character for you, as that is a valid character in a URL - though it has
special meaning (it's the fragment separator). If your filename contains a
'#' character, it will need to be escaped (into %23) to work correctly, and
you might need to do that on your own.

You should look at RFC 1738 and the documentation for Javascript's
encodeURIComponent().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top