JavaEar 专注于收集分享传播有价值的技术资料

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

Following the tutorial found on ASP.NET, implemented a Web API controller method for doing asynchronous file uploads that looks like this:

public 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);

    // Read the form data and return an async task.
    var task = Request.Content.ReadAsMultipartAsync(provider).
        ContinueWith<HttpResponseMessage>(t =>
        {
            if (t.IsFaulted || t.IsCanceled)
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        });

    return task;
}

Uploading a file via a standard multipart HTML form works perfectly. However, when another developer attempts to upload a file via multipart form constructed by Flex's FileReference class, an error is thrown:

Unexpected end of MIME multipart stream. MIME multipart message is not complete.

I have no idea if the problem lies in Web API or Flex. I've found some sort of related fixes that had no affect (Multipart form POST using ASP.Net Web API), and more recently this one ("MIME multipart stream. MIME multipart message is not complete" error on webapi upload). If the second link holds true, does anyone know if it's out in the current release of Web API available via Nuget? The discussion was in May, the most recent release from Nuget was August, so I assume this fix was deployed already, and is not the root cause of my issue.

5个回答

    最佳答案
  1. 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"

  2. 参考答案2
  3. 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.

  4. 参考答案3
  5. For those landing here googling:

    Unexpected end of MIME multipart stream. MIME multipart message is not complete.

    Reading the request stream more than once will also cause this exception. I struggled with it for hours until I found a source explaining that the request stream only could be read once.

    In my case, I combined trying to read the request stream using a MultipartMemoryStreamProvider and at the same time letting ASP.NET do some magic for me by specifying parameters (coming from the request body) for my api method.

  6. 参考答案4
  7. 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!

  8. 参考答案5
  9. Make sure the virtual directory ("~/App_Data" directory as below example) where the image files are first uploaded are physically existance. When you publish the project, it may not be in the output files.

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);