I am trying to upload an image from (DHC by Restlet) to my C# API. In the future I want to call this API from an Android or iPhone App, but I figured that I need this part working before implementing it on any such device.
This C# code is put in my api:s ValuesController. The Controller I call by default. I have made sure (via breakpoints) that the below method is accessible and called properly.
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
.. However, I get the following error when trying to post a .png to my service:
System.IO.IOException was caught
HResult=-2146232800
Message=Unexpected end of MIME multipart stream. MIME multipart message is not complete.
Source=System.Net.Http.Formatting
StackTrace:
at System.Net.Http.Formatting.Parsers.MimeMultipartBodyPartParser.<ParseBuffer>d__0.MoveNext()
at System.Net.Http.HttpContentMultipartExtensions.MoveNextPart(MultipartAsyncContext context)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Nutris.Webapi.Controllers.ValuesController.<PostFormData>d__12.MoveNext() in c:\Users\Simon\Documents\GitHub\Nutris\Nutris.Webapi\Controllers\ValuesController.cs:line 836
InnerException:
It occurs at the line:
await Request.Content.ReadAsMultipartAsync(provider);
What have I missed? It feels like I have forgotten to mention something about the mimetype for the above method.