Incorrect http headers when the download file

Project info:
Patform: Wndows

 <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="ServiceStack.Core" Version="5.8.1" />
  </ItemGroup>

===============================================

[Route("/files/{fileId}", "GET")]
public class Files {
    public string FileId{ get; set; }
}

public object Get(Files request) {
    var targetFile = getFile();
    var httpResult = new HttpResult(targetFile, asAttachment: true);
    return httpResult;
}

if the file name contains cyrillic symbols from the request headers are not sent.

like this:
fileName = “какой то файл.xls”; //not working
fileName = “sample file.xls”;//works

httpResult contains “Content-Disposition” in headers, but the headers are lost and do not reach the client

incorrect recuest headers:

as the name of the file is sent to the FileId

HTTP/1.1 200 OK
Date: Thu, 20 Feb 2020 15:30:28 GMT
Content-Type: application/excel
Server: Kestrel
Content-Length: 254745
Cache-Control: max-age=600
Last-Modified: Wed, 28 Aug 2019 07:33:10 GMT
Vary: Accept
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If
Access-Control-Allow-Methods: ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL
Access-Control-Expose-Headers: DAV, content-length, Allow
X-Powered-By: ServiceStack/5.81 NetCore/Windows

correct request headers:

HTTP/1.1 200 OK
Date: Thu, 20 Feb 2020 15:32:31 GMT
Content-Type: application/excel
Server: Kestrel
Content-Length: 254745
Cache-Control: max-age=600
Last-Modified: Wed, 28 Aug 2019 07:33:10 GMT
Vary: Accept
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Location, Lock-Token, If
Access-Control-Allow-Methods: ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL
Access-Control-Expose-Headers: DAV, content-length, Allow
X-Powered-By: ServiceStack/5.81 NetCore/Windows
Content-Disposition: attachment; filename="sample file.xls"; size=254745; creation-date=Thu 20 Feb 2020 15:00:23 GMT; modification-date=Wed 28 Aug 2019 07:33:10 GMT; read-date=Thu 20 Feb 2020 15:00:23 GMT

as you can see in the screenshot, the headers have:

the problem is on .net framework is not reproduced (test in .net 4.8)

Please help razobratsya with the problem.

Thanks in advance.

There’s not a single solution that works in all browsers but it looks like this approach from fastmail seems to have sufficiently broad support when trying to use filenames with non-ASCII chars.

This change is available from the latest v5.8.1 that’s now available on MyGet.

Аs mentioned earlier, we are already using version 5.8.1.
I don’t understand, it’s implemented in version 5.8.1 or we can solve the problem this way?

I just added the support so you need to clear your NuGet packages cache to download the latest v5.8.1 release, e.g:

$ nuget locals all -clear

This needs to be done every time you want access to any pre-release update if you’ve already got the pre-release version installed.

Got it! Works! Thank you very much Demis. :grinning:

1 Like

Hi! Unfortunately not immediately noticed, the character “space” is changed to a “+” symbol is an ASCII character, shouldn’t be a problem, it is possible to change this behavior?

for example: “какой то файл.xls” -> “какой + то + файл.xls”

Similar problem I solved in the following way, but I understand that this is not a standard solution to the problem, to change the function UrlEncode() will not correctly.

Can make the ability to override the function which generates the file name? to ability each their own to change the behavior because all languages may have their own peculiarities.

Of course if there is no solution better :grinning:

public static string ToUnicodeAscii(this string text) {
            var bytes = Encoding.UTF32.GetBytes(text);
            StringBuilder asAscii = new StringBuilder();

            for (int i = 0; i < bytes.Length; i += 4) {
                uint codepoint = BitConverter.ToUInt32(bytes, i);
                if (codepoint <= 127)
                    asAscii.Append(Convert.ToChar(codepoint));
                else
                    asAscii.AppendFormat("\\u{0:x4}", codepoint);
            }

            return asAscii.ToString();
}

Thank you in advance!

Hi Demis! If the ideas on my question?
Thank you.

I’ve changed it so it encodes spaces with %20 instead of + and have made the implementation overridable at:

ClientConfig.EncodeDispositionFileName = fileName => ...;

This change is available from the latest v5.8.1 now available on MyGet.

Thank you very much Demis!
it’s very cool!
:smiley: