![](/img/trans.png)
[英]Words with letter and numbers that can contain dashes and swedish characters
[英]Downloaded filename for Safari can't print swedish characters
我的 SPA 中有一個 function,它向 Microsoft Azure 中的服務端點發送請求,請求文件(在這種情況下為 PDF)。 下載此文件會導致文件上的名稱為,如果我錯了,請糾正我,在 MacOS 上為 Safari 編碼。
我了解到 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
};
現在使用我的 SPA,請求此文件將 output 如下:
鉻:“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
};
上面的代碼將導致:
鉻:“Brännoljegatan 4.pdf”
Safari: Br%C3%A4nnoljegatan%204.pdf"
這就是我真的不知道如何繼續讓文件名對 Safari 正確的地方。 我們的瀏覽器目標是:
可悲的是 Safari 是迄今為止唯一的害群之馬。
我希望將下載的文件命名為 Safari 的“Brännoljegatan 4.pdf”,當然其余的瑞典字符(å Å、ä Ä、ö Ö)
你有什么建議嗎?
在 canton7 和周末休息的幫助下,我能夠為我的代碼解決這個問題。
我沒有使用Response.AddHeaders()
,而是使用Headers.Add()
。
我的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.