4

I have a Web API action that accepts large (100 MB+) files and forwards them to another HTTP service. I want to stream the request body to the other service, to prevent having to wait for the entire request body to be pulled into memory.

I've found this question, which explains how to prevent Web API from buffering the request body, but I can't figure out how to access the stream from the Request object in my action.

I've looked at the functions provided by the HttpContent class, but they all imply that they pull the entire request into memory.

How can I obtain my request stream without pulling the entire body into memory?

Community
  • 1
  • 1
Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71
  • And what about `Request.Content.ReadAsStreamAsync()`? – Evk Apr 11 '17 at 20:39
  • @Evk According to the .NET Core source, it reads the request into a buffer and wraps it in a `MemoryStream` – Collin Dauphinee Apr 11 '17 at 20:45
  • But you didn't mention .net core in question? I usually just read `Request.Body` in asp.net core. – Evk Apr 11 '17 at 20:45
  • 1
    @Evk .NET 4.5 does the exact same thing. Regadless, per https://msdn.microsoft.com/en-us/library/system.net.http.httpcontent.readasstreamasync(v=vs.110).aspx: The returned Task object **will complete after all of the stream that represents content has been read**. – Collin Dauphinee Apr 11 '17 at 20:55

0 Answers0