ASP.NET API Header Content-Length for binary data

moondaddy 911 Reputation points
2022-11-04T05:51:27.887+00:00

I have a Asp.net API app built on .net 4.8 and running on windows server data center 2019.

I have an API request where an image is uploaded in the in the request content as binary data. Is it possible to get the header's content-length value for this? If so how as I'm not seeing it in the list of headers.

I have a similar API were the image is serialized and added to the request content as json and I am able to get the content-length header value for these.

Thanks.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,292 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,176 Reputation points Microsoft Vendor
    2022-11-04T08:32:30.957+00:00

    Hi @moondaddy ,

    I have an API request where an image is uploaded in the in the request content as binary data. Is it possible to get the header's content-length value for this?

    Based on your description, I think you have encapsulated the image data into DTO(Data Transfer Object). And if you want to get the Content-length, just get this parameter in Request: Request.Content.Headers.ContentLength.

    Here is a simple example:

       [HttpPost]  
               public IHttpActionResult Upload([FromBody] ImageInfo image)  
               {  
                   //get request content length  
                   long? contentLength = Request.Content.Headers.ContentLength;  
         
                   return Ok();  
               }  
    

    257170-image.png

    And the result like this:
    257136-image.png

    Hope this can help you. But if I misunderstood anything, just let me know.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments