dynamicres
New Member
I am working with Web Api to create a way to upload files via web api. I have found several blog posts on how to accomplish this, and the code is all very similar with a key commonality being the Request.Content.ReadAsMultipartAsync() call. The problem I have is the first upload works fine, but then IIS gets into a faulted state where subsequent uploads fail. The first 32Kb comes in, but then it quits. Debugging shows only a null reference exception that occurs somewhere in the ASP.NET framework.Here is the ApiController definition I have...\[code\]public class FileUploadController : ApiController{ public void Post() { if (Request.Content.IsMimeMultipartContent()) { var path = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(path); var task = Request.Content.ReadAsMultipartAsync(provider); task.ContinueWith(t => { if (t.IsFaulted || t.IsCanceled) throw new HttpResponseException(HttpStatusCode.InternalServerError); }); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted")); } }}\[/code\]Also, here is the page I am posting from...\[code\]<!doctype html><head> <title>File Upload Progress Demo #3</title></head><body> <h1>File Upload Progress Demo #3</h1> <code><input type="file" name="myfile[]"></code><br> <form action="/api/fileupload" method="post" enctype="multipart/form-data"> <input type="file" name="myfile"><br> <input type="submit" value="http://stackoverflow.com/questions/12727893/Upload File to Server"> </form> <div class="progress"> <div class="bar"></div> <div class="percent">0%</div> </div> <div id="status"></div></body>\[/code\]The above code can be downloaded in a default WebApi solution from https://github.com/JohnLivermore/FileUploadTest. Run and navigate to \[code\]http://localhost:{port}/FormPost.html\[/code\]. The first upload succeeds (uploads to App_Data), but subsequent uploads only upload the first 32 Kb and then fail.