2013-10-27 27 views
7

我一直在關注這個教程來支持文件上傳爲MVC4的的WebAPI的一部分:http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspxWebAPI上傳錯誤。預期的MIME多部分流結束。 MIME多信息是不完整的

當我的控制器接收到請求時,它抱怨說,「MIME多部分消息是不完整的。」。有沒有人有關於如何調試的提示?我已經嘗試將該流的位置重置爲0,以便在碰到處理程序之前還有別的東西正在讀取它。

我的HTML看起來像這樣:

<form action="/api/giggl" method="post" enctype="multipart/form-data"> 
    <span>Select file(s) to upload :</span> 
    <input id="file1" type="file" multiple="multiple" /> 
    <input id="button1" type="submit" value="Upload" /> 
</form> 

和我的控制器Post方法是這樣的:

public Task<IEnumerable<string>> Post() 
    { 
     if (Request.Content.IsMimeMultipartContent()) 
     { 
      Stream reqStream = Request.Content.ReadAsStreamAsync().Result; 
      if (reqStream.CanSeek) 
      { 
       reqStream.Position = 0; 
      } 

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

      var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t => 
      { 
       if (t.IsFaulted || t.IsCanceled) 
        Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception); 

       var fileInfo = streamProvider.FileData.Select(i => 
       { 
        var info = new FileInfo(i.LocalFileName); 
        return "File uploaded as " + info.FullName + " (" + info.Length + ")"; 
       }); 
       return fileInfo; 

      }); 
      return task; 
     } 
     else 
     { 
      throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!")); 
     } 
    } 

我缺少什麼明顯>

+1

修正這一點。輸入元素需要'name'元素集! – trilson

+0

這並不能幫助我呢,因爲兩者具有相同的ID,名稱仍然顯示錯誤「N錯誤occurred.Unexpected MIME多流的末尾。MIME多的消息是不System.Net.Http complete.System.IO.IOException .Formatting.Parsers.MimeMultipartBodyPartParser。 d__0.MoveNext() at System.Net.Http.HttpContentMultipartExtensions.MoveNextPart(MultipartAsyncContext context) ---以前位置的堆棧跟蹤結束,其中異常爲 – HydPhani

回答

8

你可以嘗試添加了「名「屬性到你的輸入文件?

<input name="file1" id="file1" type="file" multiple="multiple" /> 
相關問題