2016-04-06 30 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 

Repro: https://github.com/bigfont/StackOverflow/tree/master/LargeFileUploadControllerbeklenmeyen bitiş

Formu

<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> 

Geçenlerde neredeyse aynı durum var

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

Bu genellikle sınırın yanlış olduğu anlamına gelir. GetBoundary'den geçin ve doğru çalıştığından emin olun. Bunu sizin için ayırabilen Microsoft.Net.Http.Headers.MediaTypeHeaderValue da var. – Tratcher

cevap

6

Kontrolör . neredeyse derim, çünkü onlar aslında bir şey zaten vücut akışını tüketen anlamına gelir Unexpected end of Stream, the content may have already been read by another component. için yeniden adlandırıldı. Aşağıdaki değişim yorumlar bize neler anlaşılmasını sağlar:

Tratcher commented on Mar 23

... MVC modeli bağlayıcı formu okur ve sizin için çok parçalı segmentleri tamponları, bu yüzden anlamı yok içinde

Yani, soru varsayılan form bağlayıcı devre dışı bırakma olduğunu ... MultipartReader ile istek gövdesini yeniden ayrıştırma (istek formunu okuyarak)?

Ben bağlayıcı formu devre dışı bırakır bu Mvc.FileUpload sample yılında DisableFormValueModelBindingAttribute niteliğini bulundu ve bu gibi görünüyor budur:

    : Biraz daha fazla bilgi istiyorsanız

    [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) 
        { 
        } 
    } 
    

    , aşağıdaki kontrol edebilirsiniz

  • Create a filter/sample that shows how to remove the form value providers (rynowak opened this issue on Apr 26)
  • Sample: Issues with Antiforgery + Form + File Uploads (rynowak opened this issue on Apr 26)

Sadece bilgi için - daha önce yorumlandığı gibi, MVC model bağlayıcısı formunu okur, ancak sonuçları nereden bulabilirim. Sonuçlar Files olan HttpRequest.Form'da bulunabilir.

1

Bu durumun size yardımcı olup olmayacağını bilemezsiniz, ancak "Aktarımın Beklenmedik Bitiş Noktası, içerik zaten başka bir bileşen tarafından okunmuş olabilir" gibi simüler sorunlara rastladım. Başlangıç ​​kodu Startup.cs Yapılandırma yöntemine eklenmiştir.

Yardım edin,