2

I have this HttpClient for posting files:

var content = new MultipartFormDataContent(Guid.NewGuid().ToString())
{
    new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json")
};

foreach (var file in files)
{
    var byteContent = new ByteArrayContent(file.Data);

    content.Add(byteContent, file.FieldName, file.FileName);
}

return await _client.PostAsync(_apiUrl + apiRelativeUrl, content);

But my ASP.NET WebApi Controller gives error 500 with this exception:

System.IO.IOException: Unexpected end of MIME multipart stream. MIME multipart message is not complete.

The WebApi controller code is

if (Request.Content.IsMimeMultipartContent())
{
    var multipart = await Request.Content.ReadAsMultipartAsync(); //this line throws the exception
    var fileContent = multipart.Contents.FirstOrDefault();

    if (fileContent != null)
    {
        var photo = await fileContent.ReadAsByteArrayAsync();
    }
}

What am I doing wrong?

7
  • Please provide complete code to understand the issue. Commented Aug 26, 2018 at 9:59
  • 1
    What code do you need? The var parameters is a generic, the files is IEnumerable<> of a custom class with byte[] data, field name and file name. Commented Aug 26, 2018 at 10:10
  • What’s your controller signature? Where do you get your files? What’s your MVC/jQuery code? Commented Aug 26, 2018 at 10:15
  • @Caelan Can you access web api method code? can you take break and trace? which line you got error? Commented Aug 26, 2018 at 10:30
  • 1
    It’s a webapi controller reading data from Request, files came from phone camera, Xamarin forms app Commented Aug 26, 2018 at 10:31

3 Answers 3

2

Basically I used a custom stream to append the newline that asp.net web api is expecting.

Stream reqStream = Request.Content.ReadAsStreamAsync().Result;
MemoryStream tempStream = new MemoryStream();
reqStream.CopyTo(tempStream);



tempStream.Seek(0, SeekOrigin.End);
StreamWriter writer = new StreamWriter(tempStream);
writer.WriteLine();
writer.Flush();
tempStream.Position = 0;


 StreamContent streamContent = new StreamContent(tempStream);
 foreach(var header in Request.Content.Headers)
 {
     streamContent.Headers.Add(header.Key, header.Value);
 }

// Read the form data and return an async task.
 await streamContent.ReadAsMultipartAsync(provider);

and add following code in web.config

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

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you think it's a server problem and not a client one? Updating the web app requires more work from me so I'm trying to send the right data. I tried to add the writeline to the file stream but the response has not changed.
@Caelan I think server-side api have some problem. If you're not sure, First Test api with Postman
I tried debugging the web server using linqpad for making requests. I've added the code you suggested but nothing has changed.
1

Try add header of the files type you upload like this:

 byteContent.Headers.ContentType =
            MediaTypeHeaderValue.Parse("image/jpeg");

This works fine for me.

Comments

1

I created two new empty projects, one for webapi and one for httpclient.

I tried copying the existing client code and the existing server code to the new projects.

I discovered that the problem was the parameter to the webapi action, it seems to broke the stream.

So if someone need to upload multipart form data to ASP.NET WebApi don't use parameters in the action, it won't resolve any model and it broke the multipart stream.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.