2

My application allow users to download files, but names of files can be in cyrillic encoding. When user downloads file i want name to be like user see, but in controller ContentDisposition doesn't allow name in cyrillic encoding, and i try to convert it to utf-8. Chrome, IE and opera downloads file with correct name:

enter image description here firefox and safari with something like this


enter image description here

my controller:

 public ActionResult Download()
        {
            var name = Request.Params["Link"];
            var filename = Request.Params["Name"];


            filename = GetCleanedFileName(filename);

            var cd = new ContentDisposition
            {
                FileName = filename,
                Inline = false,
            };


            Response.AppendHeader("Content-Disposition", cd.ToString());
            return File(name, "application/file");
        }


        public static string GetCleanedFileName(string s)
        {
            char[] chars = s.ToCharArray();

            StringBuilder sb = new StringBuilder();

            for (int index = 0; index < chars.Length; index++)
            {
                string encodedString = EncodeChar(chars[index]);
                sb.Append(encodedString);
            }
            return sb.ToString();
        }

        private static string EncodeChar(char chr)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            StringBuilder sb = new StringBuilder();

            byte[] bytes = encoding.GetBytes(chr.ToString());

            for (int index = 0; index < bytes.Length; index++)
            {
                if (chr > 127)
                    sb.AppendFormat("%{0}", Convert.ToString(bytes[index], 16));
                else
                    sb.AppendFormat("{0}", chr);

            }

            return sb.ToString();
        } 
casperOne
  • 73,706
  • 19
  • 184
  • 253
Andriy Khrystyanovich
  • 1,422
  • 3
  • 19
  • 38
  • possible duplicate of [How to encode the filename parameter of Content-Disposition header in HTTP?](http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http). After reading the answers in this duplicate question you will realize the extent of the problem and will probably simply rename your files to use only ASCII characters in their names. – Darin Dimitrov Nov 03 '11 at 17:29
  • Unfortunately I can't rename files. – Andriy Khrystyanovich Nov 03 '11 at 17:37
  • well then, good luck reading the specification: http://greenbytes.de/tech/webdav/rfc5987.html Oh, and of course if you need to support browsers that are not conform to this specification, well, you will have to handle them separately (and this for every browser and version you want to support). Another possibility is to compress the document into a .zip file with an ASCII name that you will stream to the client and inside will be the actual document with any characters in its name. – Darin Dimitrov Nov 03 '11 at 17:40

1 Answers1

1

Look here, we used this as an example to return the filename that we wished when downloading files through a controller.

http://www.simple-talk.com/dotnet/asp.net/asp.net-mvc-action-results-and-pdf-content/

The FileResult has a FileDownloadName property that you can set that it uses to set the Filename as it's sent to the Response when the user downloads it.

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62