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?