2

I'm using this code to process a file upload to a web api:

[HttpPost]
public async Task<IHttpActionResult> Post(string provider)
{  
    if (!Request.Content.IsMimeMultipartContent())
        throw new Exception();

    var streamProvider = new MultipartMemoryStreamProvider();
    await Request.Content.ReadAsMultipartAsync(streamProvider); // FAILS HERE
    foreach (var file in streamProvider.Contents)
    {
        var imageFilename = file.Headers.ContentDisposition.FileName.Trim('\"');
        var imageStream = await file.ReadAsStreamAsync();

    }
}

but it throws an error here: await Request.Content.ReadAsMultipartAsync(streamProvider);

The error is: Error reading MIME multipart body part. The inner Exception is:

{"Cannot access a disposed object."}

any ideas on why this error is coming up?

5
  • better add try catch blocks Commented Jun 5, 2014 at 18:35
  • 1
    @user3629247 He has the exception already why would he need a trycatch? he wouldnt be posting here if he was ignoring the problem Commented Jun 5, 2014 at 18:39
  • @user441365 are you using the right Content-Type header value?? aka Content-Type: multipart/form-data Commented Jun 5, 2014 at 18:40
  • if we wrap our method in try-catch block,we can gracefully handle any errors that occur as part of the method. for this scenario he already catch the exception Commented Jun 5, 2014 at 18:42
  • Are you using the enctype attribute? Commented Jul 5, 2014 at 1:54

4 Answers 4

8

I had a similar problem but the provided solution did not work for me, so here is mine : the file you have uploaded did not match the security limits of IIS.

http://forums.asp.net/t/2062896.aspx?Error+reading+MIME+multipart+body+part+when+upload+image

<system.web>
    <httpRuntime maxRequestLength="30000000" /> <!-- if you forget this one it does not work -->
</system.web>

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="30000000" />
      </requestFiltering>
    </security>
</system.webServer>
Sign up to request clarification or add additional context in comments.

Comments

4

I had the same exception, but for a different reason. My Post method had a return type of void. Once I changed the return type to Task<[T]>, type of string in my case, it started working.

1 Comment

This is the common .NET "async void" problem (i.e you can not have an async method to return void) : msdn.microsoft.com/en-us/magazine/jj991977.aspx
3

Have you tried this?

[HttpPost]
public async Task<IHttpActionResult> Post(string provider)
{  
    if (!Request.Content.IsMimeMultipartContent())
        throw new Exception();

    var streamProvider = await Request.Content.ReadAsMultipartAsync(); // HERE
    foreach (var file in streamProvider.Contents)
    {
        var imageFilename = file.Headers.ContentDisposition.FileName.Trim('\"');
        var imageStream = await file.ReadAsStreamAsync();
    }
}

It looks as it should be called like this.

1 Comment

If I copy this code exactly, I get the disposed error right on the line with the // HERE comment :/
0

Shouldn't you have path to store uploaded files. Something like this:

var streamProvider = new MultipartMemoryStreamProvider(@"C:\Uploads");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.