Thursday 19 January 2017

Error reading MIME multipart body part when upload a multipart-formdata in webapi controller resolved

In this post we are going to see solution for one error when you try to upload a image in a web api method, if you get a error like Error reading MIME multipart body part when upload image in web api controller when executing the following line then do the following steps.

var streamprovider = new MultipartFormStreamProvider(path);
await Request.Content.ReadAsMultipartAsync(streamprovider);


1. It may be because of memory issue, please increase the memory in config.

<system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="30000000" />
      </requestFiltering>
    </security>
</system.webServer>



  <system.web>
    <httpRuntime targetFramework="4.6.1" maxRequestLength="30000000"  />  

  </system.web>



If the issue is not resolved, then try next step.



2. It may be the issue because of CRLF  \r\n issue, that means some unexpected character at the end of file missing or added, please check it using the fiddler, it is a bug reported to microsoft, it is fixed latest version, please take latest version

if the issue is not resolved , then try next step.



3. Please see inner exception, whether it is denoting something like stream is disposed, after the execution of certain line , then use the alternate way to get info.

  var path = HttpContext.Current.Server.MapPath("~/img");
  var streamprovider = new MultipartFormStreamProvider(path);
  await Request.Content.ReadAsMultipartAsync(streamprovider);

instead



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




From this article you can resolve one issue regarding the MIME multipart body part reading error.



No comments:

Post a Comment