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

I had the same problem with MVC4, but Will is correct, add a name to your input.....

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

and all the magic is back up and working!


I had the same problem with flex. And below is the code that solved it. 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);

Hope this helps.


Reading through your existing research and following through to the codeplex issue reported it looks like someone else confirmed this issue to still exist in September.

They believe that MVC 4 fails to parse uploads without a terminating "\r\n".

The issue is really simple but extremely hard to fix. The problem is that Uploadify does > not add an "\r\n" at the end of the MultiPartForm message

http://aspnetwebstack.codeplex.com/discussions/354215

It may be worth checking that the Flex upload adds the "\r\n"