WebAPI cannot parse multipart/form-data post
My application was experiencing this error periodically too. Upgrading to WEB API 2.1 did nothing and the exception message is completely useless.
I think what was actually happening though is it was choking on large files. Increasing my max request limits in web.config seemed to fix it right up.
<system.web> <httpRuntime maxRequestLength="30000" /></system.web><system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="30000" /> </requestFiltering> </security></system.webServer>
(This sets the ceiling to 30 megs. Set it to whatever you need. More info here)
I encountered this error too. The InnerException
is Cannot access a disposed object.
This means that something is reading your stream before your call to ReadAsMultipartAsync
.
Somewhere before this call Request.Content.ReadAsMultipartAsync(provider)
, you can callRequest.Content.LoadIntoBufferAsync().Wait()
, which will load this tream into a buffer and allow you to read it more than once.
This is not an optimal solution, but it works.
I am leaving this here since it took me quite some time trying other workarounds until I bumped onto the following helpful answer and some people having this issue may end up on this post.
A \r\n needs to be appended at the end of request content stream.
Instead of using this line to read the data:
await Request.Content.ReadAsMultipartAsync(provider);
You will need to:
load the request stream to memory
append the \r\n string that is required
create a stream content from the memory content
manually add the request headers to the stream content
Finally use this instead:
streamContent.ReadAsMultipartAsync(provider);
Check the answer of Landuber Kassa here for the complete code: ASP.NET Web API, unexpected end of MIME multi-part stream when uploading from Flex FileReference