2016-04-06 205 views
2
System.IO.IOException: Unexpected end of stream. 
at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.<ReadAsync>d__32.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.<DrainAsync>d__2.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at Microsoft.AspNetCore.WebUtilities.MultipartReader.<ReadNextSectionAsync>d__14.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
at AspNetCoreFileUpload.Controllers.FileUploadController.<Index>d__0.MoveNext() 
in C:\\GitHub\\StackOverflow\\LargeFileUploadController\\FileUploadController.cs:line 29 

攝製:https://github.com/bigfont/StackOverflow/tree/master/LargeFileUploadController意外結束

形式

<form action = ""/FileUpload"" method=""post"" enctype=""multipart/form-data""> 
    <label for=""myfile1"">File</label> 
    <input type=""file"" name=""myFile1"" /> 
    <label for=""myfile2"">File</label> 
    <input type=""file"" name=""myFile2"" /> 
    <input type=""submit"" value=""Send"" /> 
</form> 

控制器

public class FileUploadController : Controller 
{ 
    [HttpPost] 
    public async Task<IActionResult> Index() 
    { 
     var boundary = GetBoundary(Request.ContentType); 
     var reader = new MultipartReader(boundary, Request.Body); 

     try 
     { 
      var section = await reader.ReadNextSectionAsync(); 
     } 
     catch (System.Exception ex) 
     { 
      return new OkObjectResult(new { ex = ex.ToString() }); 
     } 

     return new OkObjectResult(new { message = "Done" }); 
    } 

    private static string GetBoundary(string contentType) 
    { 
     var elements = contentType.Split(' '); 
     var element = elements.Where(entry => entry.StartsWith("boundary=")).First(); 
     var boundary = element.Substring("boundary=".Length); 
     // Remove quotes 
     if (boundary.Length >= 2 && 
      boundary[0] == '"' && boundary[boundary.Length - 1] == '"') 
     { 
      boundary = boundary.Substring(1, boundary.Length - 2); 
     } 
     return boundary; 
    } 
} 
+1

這通常意味着你有邊界錯誤。逐步通過GetBoundary並確保它正常工作。還有可以爲你解析這個的Microsoft.Net.Http.Headers.MediaTypeHeaderValue。 – Tratcher

回答

6

幾乎相同的異常最近。我在說差不多,因爲它們實際上將異常重命名爲Unexpected end of Stream, the content may have already been read by another component.,這實際上意味着某些東西已經消耗了正文流。以下更改的意見使我們發生了什麼事的理解:

Tratcher commented on Mar 23

...的MVC模型綁定讀取的形式和緩衝多段 你,所以沒有點在重新解析請求主體與 MultipartReader ...

所以,問題是如何禁用默認的形式結合(閱讀請求表單)?

我發現DisableFormValueModelBindingAttribute屬性在此Mvc.FileUpload sample它禁用的形式結合,這是什麼樣子:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter 
{ 
    public void OnResourceExecuting(ResourceExecutingContext context) 
    { 
     var formValueProviderFactory = context.ValueProviderFactories 
       .OfType<FormValueProviderFactory>() 
       .FirstOrDefault(); 
     if (formValueProviderFactory != null) 
     { 
      context.ValueProviderFactories.Remove(formValueProviderFactory); 
     } 

     var jqueryFormValueProviderFactory = context.ValueProviderFactories 
      .OfType<JQueryFormValueProviderFactory>() 
      .FirstOrDefault(); 
     if (jqueryFormValueProviderFactory != null) 
     { 
      context.ValueProviderFactories.Remove(jqueryFormValueProviderFactory); 
     } 
    } 

    public void OnResourceExecuted(ResourceExecutedContext context) 
    { 
    } 
} 

如果你想要更多的信息,你可以檢查出以下幾點:

僅供參考 - 如前所述,MVC模型綁定器讀取表格,但哪裏可以找到結果。結果可以在HttpRequest.Form中找到,其中有Files

1

不知道這是否可以幫助你,但我遇到了類似問題「流的意外結束,內容可能已被其他組件讀取」。

app.Use(async (context, next) => { 
      context.Request.EnableRewind(); 
      await next(); 
     }); 

上面的代碼被添加到Startup.cs配置方法中。

希望它有幫助