When running this on my dev machine no issues, works great. When deployed to my server I always get the error.
System.IO.IOException: Error reading MIME multipart body part. ---> System.InvalidOperationException: There is a pending asynchronous operation, and only one asynchronous operation can be pending concurrently. at System.Web.Hosting.IIS7WorkerRequest.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
I am attempting to upload an image. When I run this on my local machine no errors and the file does get uploaded.
public async Task<HttpResponseMessage> PostFile(int TaskID)
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
try
{
StringBuilder sb = new StringBuilder(); // Holds the response body
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
//// Read the form data and return an async task.
await Request.Content.ReadAsMultipartAsync(provider);
The error hits the last line of the code that begins with await.
Needed to add the following line of code.
Request.Content.LoadIntoBufferAsync().Wait();
This was added just before the line that was erroring out.
So it now looks like this.
var provider = new MultipartFormDataStreamProvider(root);
Request.Content.LoadIntoBufferAsync().Wait();
//// Read the form data and return an async task.
await Request.Content.ReadAsMultipartAsync(provider);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With