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:
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?
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);