Convert file path to URL

  • Thread starter Thread starter Anders K. Olsen
  • Start date Start date
A

Anders K. Olsen

Hello Group

I have a fileserver with several files. Some of the filenames contain danish
characters (e.g. 'æ', 'ø', 'å'). Now I'm trying to make a web application in
C# that presents links to some of the files. I would like the user to be
able to open the file by clicking on the link.

I have the file:

"G:\My Documents\Anders Kåre Olsen.txt"

If I use this path as a link in MSIE, it automatically converts it to:

file:///G:/My%20Documents/Anders%20K%E5re%20Olsen.txt

However Firefox does not do this conversion automatically, and I have a
requirement that says that the page must work in MSIE and Firefox. If I copy
the URL from MSIE to Firefox, it works fine, so I just need to do the
conversion programmatically (in this case, I need to convert ' ' to '%20'
and 'å' to '%E5').

I have tried to use the HttpUtility.UrlPathEncode, but this method
apparantly have problems with the danish characters like 'å'. It converts
last path of the above path to:

/My%20Documents/Anders%20K%c3%a5re%20Olsen.txt

And not even MSIE likes the conversion of 'å' to %c3%a5.

Basically, is there an automatic way to convert the characters to something
that both browsers like. And why does C# convert 'å' to %c3%a5? Is it
because of unicode?

Regards

Anders Olsen
 
Hi,

I am not 100% sure this will solve your problem. Assume that you have
the path in a string called 'str', you might want to try:

string result;
result = System.Web.HttpUtility.UrlEncode (
System.Text.Encoding.GetEncoding ( "ISO-8859-1" ).GetBytes ( str ) );

I think all danish characters should be covered by the ISO-8859-1 codepage.

-Lenard
 
Lenard Gunda said:
Hi,

I am not 100% sure this will solve your problem. Assume that you have the
path in a string called 'str', you might want to try:

string result;
result = System.Web.HttpUtility.UrlEncode (
System.Text.Encoding.GetEncoding ( "ISO-8859-1" ).GetBytes ( str ) );

I think all danish characters should be covered by the ISO-8859-1
codepage.

Yes, all danish characters are covered by ISO-8859-1.

I almost worked. The only problem left was that UrlEncode converted a space
(' ') to a plus ('+'), and the browsers didn't like that. However it was
easy to use String.Replace("+","%20") on the result.

Thank you for your help.

Regards
Anders
 

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

Back
Top