Download MVC 4 Web API.NET 4 File

I am using the MVC version that comes with Visual Studio 2012 express. (Microsoft.AspNet.Mvc.4.0.20710.0)

I assume this is a version of RTM.

I found many examples online that all use this code:

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) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception); } // 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); }); return task; } 

But this code always ends in continueWith where t.IsFaulted == true . An exception:

Unexpected end of multi-threaded MIME stream. The MIME multipart message is not complete.

Here is my client form. NOTHING NECESSARY, I want to use jquery form pluging to load ajax, but I can't even work that way.

 <form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" > <input type="file" /> <input type="submit" value="Upload" /> </form> 

I read that this is caused by a parser waiting for / CR / LF at the end of each message, and this bug was fixed in June.

What I canโ€™t understand, if this was really fixed, why is it not included in this version of MVC 4? Why do so many examples on the Internet say that this code works when it is not in this version of MVC 4?

+8
asp.net-web-api asp.net-mvc-4
source share
1 answer

Your input file is missing the name attribute.

 <form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" > <input name="myFile" type="file" /> <input type="submit" value="Upload" /> </form> 

Entries without it will not be sent by the browser. Thus, your formdata is empty, which leads to an IsFaulted statement.

+19
source share

All Articles