1

I have a function in my SPA that sends a request to our service endpoint in Microsoft Azure, requesting a file (PDF in this case). Downloading this file causes the name on the file to be, correct me if I'm wrong, as encoded for Safari on MacOS.

I have come to an understanding that Safari or MacOS uses UTF8, a string written in .net framework on a windowsmachine would be UTF16, but for test purposes I'm writing the filename directly into the ContentDispositionHeaderValue, code sample below:

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(data)
        };

        result.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
        { 
            FileName = "Brännoljegatan 4.pdf", 
            CreationDate = DateTimeOffset.UtcNow 
        };

Using my SPA now, requesting this file would output as follow:
Chrome: "Brännoljegatan 4.pdf"
Safari: =?utf-8?B?QnLDpG5ub2xqZWdhdGFuIDQucGRm?=

        string fileName = Uri.EscapeDataString("Brännoljegatan 4.pdf");

        result.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
        { 
            FileName = fileName, 
            CreationDate = DateTimeOffset.UtcNow 
        };

Code above would result in:
Chrome: "Brännoljegatan 4.pdf"
Safari: Br%C3%A4nnoljegatan%204.pdf"

This is where I really don't know how to continue getting the filename to be correct for Safari. Our browser targets are:

  • Chromium based (Chrome, MS Edge)
  • Internet Explorer 11
  • Safari

Sadly Safari is the only black sheep so far.

I want the downloaded file to be named "Brännoljegatan 4.pdf" for Safari, of course the remaining swedish characters (å Å, ä Ä, ö Ö)
Do you have any suggestions?

Kokefa
  • 85
  • 9
  • 1
    Have you seen [this answer](https://stackoverflow.com/a/6745788/1086121)? – canton7 Jul 03 '20 at 13:38
  • 1
    I had seen it, I wasn't successful to make it work but after a weekends break I managed to fix it within minutes, I will add a answer below! – Kokefa Jul 06 '20 at 07:06

1 Answers1

1

With help from canton7 and a weekends break I was able to solve this problem for my code.
Instead of using Response.AddHeaders(), I used Headers.Add().
My result is a HttpResponseMessage and using .NET Framework 4.7.

            string fileName = "Brännoljegatan 4.pdf";
            string mediaType = "application/pdf";

            string contentDisposition;
            contentDisposition = "attachment; filename=\"" + fileName + "\"; filename*=UTF-8''" + Uri.EscapeDataString(fileName);

            result.Content.Headers.Add("Content-Disposition", contentDisposition);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
Kokefa
  • 85
  • 9