FileStreamResult problem returning files with certain names

Viewed 1459

in my function, I return a FileStreamResult, but depending on the file name, the downloaded file gets a weird name (in some browsers returns the function name and in others the file name but all mixed)

public FileStreamResult _GetFile(long idFile)
{
    //////////
    /// ... get the object 'file'
    //////////
    
    FileStreamResult retorno = null;
    if (file != null)
    {
        var stream = new MemoryStream(file.Bytes);
        retorno = File(stream, "binary", file.Name);
    }

    return retorno;
}

When the file name has accents and a certain size the error occurs (in chrome, for example, downloads the file with the name "_GetFile")

if the file name it's something like this

áá ââ_abcdefghijlmnpqrstuvxzkwy_abcdefghijlmnopqrstuvxzkwy.abc

it generates the error

but these two don't

áá ââ_abcdefghijlmnpqrstuvxzkwy.abc

aa aa_abcdefghijlmnpqrstuvxzkwy_abcdefghijlmnopqrstuvxzkwy.abc

UPDATE

I noticed that the Content-Disposition of my response Header when doesn't "work" looks like this

Content-Disposition: attachment; filename="=?utf-8?B?w6HDoSAgw6LDol9hYmNkZWZnaGlqbG1ucHFyc3R1dnh6a3d5X2FiY2Rl?=%0d%0a =?utf-8?B?ZmdoaWpsbW5vcHFyc3R1dnh6a3d5LmZwcg==?="

showing two ?utf-8?, but I still don't understand why.

(When it works and the name have accents it shows only one ?utf-8?)

Any help will be appreciated!

Thanks!

1 Answers

FileStreamResult has a property called FileDownloadName so you need to specify that.

public FileStreamResult _GetFile(long idFile)
{
    //////////
    /// ... get the object 'file'
    //////////

    if (file != null)
    {
        var stream = new MemoryStream(file.Bytes);
        retorno = File(stream, "binary", file.Name);
    }
    var fileName = "Your name here";
    return new FileStreamResult(stream, "binary") {FileDownloadName = fileName;}
}
Related