C# – asp.net mvc file download –System.FormatException: An invalid character was found in the mail header

asp.net-mvcc

Our website has files in a few different languages – French, Spanish, Portuguese, and English. When a user uploads a file that contains special characters like ó or ç or ã etc i get an error message when i return File(data, "application/octet-stream", name); in MVC i get the exception:

System.FormatException: An invalid character was found in the mail header.

I found an article in MSDN for this showing how to set the mailmessage to UTF-8 encoding to avoid this. But i do not know how to UTF-8 encode the filename when using the MVC file actionresult. I found an article on the net to UTF-8 encode a string but when I try to use it I get a garbage name so I guess I do not understand what UTF-8 encoding is supposed to do to the string. Here is the sample code found in this blog post: An invalid character was found in the mail header

 public static string GetCleanedFileName(string s)
    {

        char[] chars = s.ToCharArray();

        var 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)
    {

        var encoding = new UTF8Encoding();

        var sb = new StringBuilder();

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

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

Best Answer

Maybe try another function encoding from and to utf8


//UTF8
        public static string ConvertToUTF8(string inputString)
        {
            string toReturn = "";
            byte[] arr = Encoding.UTF8.GetBytes(inputString);
            for (int i = 0; i < arr.Length; i++)
            {
                toReturn += arr[i].ToString() + " ";
            }

            return toReturn;
        }

        public static string ConvertFromUTF8(string inputString)
        {
            inputString = inputString.Trim();
            string result = "";
            string[] parts = inputString.Split(' ');
            byte[] bytes = new byte[parts.Length];
            for (int i = 0; i < parts.Length; i++)
            {
                if (parts[i] == "")
                {
                    continue;
                }
                try
                {
                    bytes[i] = Convert.ToByte(parts[i]);
                }
                catch (Exception)
                {
                    MessageBox.Show("Input string was not in a correct format.");
                }

            }

            try
            {
                result = Encoding.UTF8.GetString(bytes);
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }