3

I have one problem.

In ASP.NET application i created link to some document, document name is stored in database and when user click on link File Download dialog appears.

Problem occurs when file name is Serbian Cyrilic, File Download dialog shows file name with some strange characters. See image

File download file name strange characters

Whene i use HtmlEncode for file name IE works fine (shows right file name), but then problem is in FireFox.

Thanks.

buda
  • 2,372
  • 1
  • 18
  • 23
  • possible duplicate of [How to display a non-ascii filename in the file download box in browsers?](http://stackoverflow.com/questions/149058/how-to-display-a-non-ascii-filename-in-the-file-download-box-in-browsers) – devnull Mar 08 '14 at 08:20

1 Answers1

2

You have to encode non-AscII characters. I'm using this method:

    public static string URLEncode(string tekst)
    {
        byte[] t = Encoding.UTF8.GetBytes(tekst);
        string s = "";
        for (int i = 0; i < t.Length; i++)
        {
            byte b = t[i];
            int ib = Convert.ToInt32(b);
            if (ib < 46 || ib > 126)
            {
                s += "%" + ib.ToString("x");
            }
            else
            {
                s += Convert.ToChar(b);
            }
        }
        return s;
    }  

And check if you have to encode it - it should work then in IE and FF:

if (Page.Request.Browser.IsBrowser("IE"))  
fileName = URLEncode(fileName);
Marek Kwiendacz
  • 9,524
  • 14
  • 48
  • 72
  • 2
    Ok thanks, but you already have method for UrlEncode HttpUtility.UrlEncode(tekst), you didn hade to write your own. I worked on this way, but i wanted to see if there is some, maybe better solution. – buda Apr 01 '11 at 13:06