Unicode in Content-Disposition header

后端 未结 7 1403
情歌与酒
情歌与酒 2020-12-03 17:46

I am using HttpContext object implemented in HttpHandler child to download a file, when I have non-ascii characters in file name it looks weird in IE whereas it looks fine i

7条回答
  •  暖寄归人
    2020-12-03 18:19

    For me this solved the problem:

    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
       Content = new ByteArrayContent(data)
    };
    
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileNameStar = "foo-ä-€.html"
    };
    

    When i look ad the repsonse in fiddler i can see the filename has automaticcaly been encoded using UTF-8:

    Fiddler response example with encoded Content-Disposition filename using UTF-8

    If we look at the value of the Content-Disposition header we can see it will be the same as @Johannes Geyer his answer. The only difference is that we didn't have to do the encoding ourselfs, the ContentDispositionHeaderValue class takes care of that.

    I used the Testcases for the Content-Disposition header on: http://greenbytes.de/tech/tc2231/ as mentioned by Julian Reschke. Information about the ContentDispositionHeaderValue class can be found on MSDN.

提交回复
热议问题