简体   繁体   English

Safari 的下载文件名无法打印瑞典语字符

[英]Downloaded filename for Safari can't print swedish characters

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).我的 SPA 中有一个 function,它向 Microsoft Azure 中的服务端点发送请求,请求文件(在这种情况下为 PDF)。 Downloading this file causes the name on the file to be, correct me if I'm wrong, as encoded for Safari on MacOS.下载此文件会导致文件上的名称为,如果我错了,请纠正我,在 MacOS 上为 Safari 编码。

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:我了解到 Safari 或 MacOS 使用 UTF8,在 windowsmachine 上用 .net 框架编写的字符串将是 UTF16,但出于测试目的,我将文件名直接写入ContentDispositionHeaderValue ,代码示例如下:

        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:现在使用我的 SPA,请求此文件将 output 如下:
Chrome: "Brännoljegatan 4.pdf"铬:“Brännoljegatan 4.pdf”
Safari: =?utf-8?B?QnLDpG5ub2xqZWdhdGFuIDQucGRm?= 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"铬:“Brännoljegatan 4.pdf”
Safari: Br%C3%A4nnoljegatan%204.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.这就是我真的不知道如何继续让文件名对 Safari 正确的地方。 Our browser targets are:我们的浏览器目标是:

  • Chromium based (Chrome, MS Edge)基于铬(Chrome、MS Edge)
  • Internet Explorer 11互联网浏览器 11
  • Safari Safari

Sadly Safari is the only black sheep so far.可悲的是 Safari 是迄今为止唯一的害群之马。

I want the downloaded file to be named "Brännoljegatan 4.pdf" for Safari, of course the remaining swedish characters (å Å, ä Ä, ö Ö)我希望将下载的文件命名为 Safari 的“Brännoljegatan 4.pdf”,当然其余的瑞典字符(å Å、ä Ä、ö Ö)
Do you have any suggestions?你有什么建议吗?

With help from canton7 and a weekends break I was able to solve this problem for my code.在 canton7 和周末休息的帮助下,我能够为我的代码解决这个问题。
Instead of using Response.AddHeaders() , I used Headers.Add() .我没有使用Response.AddHeaders() ,而是使用Headers.Add()
My result is a HttpResponseMessage and using .NET Framework 4.7.我的result是一个HttpResponseMessage并使用 .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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM