2

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.

Filip
  • 3,002
  • 1
  • 26
  • 37
Eyeball
  • 3,267
  • 2
  • 26
  • 50

2 Answers2

0

Check the Inputs that are sending the data.

Make sure it has their basic attributes set like:

  • name:
  • type:
  • value: (I don't think this was is necessary.)

    <input type="file" name="dataFile" value="Choose..."  multiple/>
    

These are all necessary for the MIME to be considered complete and can be mapped correctly by your handler.

You should post your HTML side of code, to make sure it is the same issue.

S.H.
  • 2,833
  • 3
  • 26
  • 28
0

Simon, try adding 'name' to your input control, you may be missing that.

<input type="file" id="fileInput" name="fileInput"/> 

ASP.NET Web API, unexpected end of MIME multi-part stream when uploading from Flex FileReference

Community
  • 1
  • 1
Illuminati
  • 4,539
  • 2
  • 35
  • 55