Problem downloading a file with an accented name

Asked

Viewed 133 times

1

I have a function that returns a file to download, when the file name has accented characters and a certain size, the downloaded file comes with the wrong name (in Chrome comes the name of my function and in Firefox comes the shuffled name),

public FileStreamResult _ObtemArquivo(long idArquivo)
{
    DtoArquivo arquivo = null;
    //  ---------------------
    //  ... Obtém o "arquivo"
    //  ---------------------

    FileStreamResult retorno = null;
    if (arquivo != null)
    {
        var stream = new MemoryStream(arquivo.Bytes);

        //  Usando o nome do arquivo diretamente o erro ocorre em todos os Browsers.
        retorno = File(stream, "binary", arquivo.Nome);

        //retorno = File(stream, "binary", Uri.EscapeUriString(arquivo.Nome));
    }

    return retorno;
}

Using "Uri.Escapeuristring" the error does not occur in Chrome but in Firefox all files with accent in name comes wrong (showing the formatting in name, Ex: %C3%A1%C3%A1%20%20C3%A2C3%A2_abcdefghijlmnpqrstuvxzkwy_abcdefghijlmnopqrstuv).

I also tested with HttpUtility.UrlEncode and Uri.EscapeDataString and both did not work as desired on all browsers.

When the file name has accent but is a short name, for example,

áá âââ_abcdefghijlmnpqrstuvxzkwy.abc

no problem, the Content-Disposition generated in Response header is

Conten-Disposition: Attachment; filename="=? utf-8? B? w6HDoSAgw6LDol9hYmNkZWZnaGlqbG1ucHFyc3R1dnh6a3d5LmZwcg==?="

only when the name is larger, for example,

áá âââ_abcdefghijlmnpqrstuvxzkwy_abcdefghijlmnopqrstuvxzkwy.abc

the error is generated and the Conten-Disposition generated is

Conten-Disposition: Attachment; filename="=? utf-8? B? w6HDoSAgw6LDol9hYmNkZWZnaGlqbG1ucHFyc3R1dnh6a3d5X2FiY2Rl?= %0d%0a =?utf-8? B? Zmdoawpsbw5vchfyc3r1dnh6a3d5lmcg==?="

I believe the problem is caused because in the second Conten-Disposition has two ?utf-8?, but I still don’t understand why this is happening and how to solve this problem.

Any help to understand or circumvent this problem will be very welcome, Thanks in advance.

1 answer

0

I’m new to the subject, but you’ve tried using Namespace: System.Globalization.

And in the file for download Cultureinfo.Invariantculture And why I had a similar problem and with that namespce solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.