HttpResponseMessage.Content.Headers ContentDisposition is null

后端 未结 2 1064
刺人心
刺人心 2021-01-07 18:49

When downloading a file with HttpClient, I\'m downloading first the headers and then the content. When headers are downloaded, I can see Headers collection on the Content pr

相关标签:
2条回答
  • 2021-01-07 19:12

    The problem is with the trailing ; in the content-disposition header

            [Fact]
            public void ParseContentDispositionHeader()
            {
                var value = ContentDispositionHeaderValue.Parse("attachment; filename=GeoIP2-City_20140107.tar.gz");
                Assert.Equal("GeoIP2-City_20140107.tar.gz",value.FileName);
            }
    

    If I add the semi-colon the parsing will fail. If you look at the RFC6266 grammar, the semi-colon is only supposed to precede the parameter.

    0 讨论(0)
  • 2021-01-07 19:24

    Thank you - finding this definitely helped me. For the benefit of others, here is my workaround (as apparently this is still a thing today???)

    I am in a somewhat controlled environment, so the following code assumes:

    • Only one Content-Disposition Header
    • The tag is in the format: inline; "filename";

    This will reset the response's ContentDisposition header, so subsequent code works seamlessly:

    <!-- language: c# -->
    if (response.Content.Headers.ContentDisposition == null)
    {
      IEnumerable<string> contentDisposition;
      if (response.Content.Headers.TryGetValues("Content-Disposition", out contentDisposition))
      {
       response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition.ToArray()[0].TrimEnd(';').Replace("\"",""));
      }
    }
    
    0 讨论(0)
提交回复
热议问题