Microsoft.AspNetCore.WebUtilities.MultipartReaderStream中意外的流结束

Sha*_*tin 5 asp.net-core

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
Run Code Online (Sandbox Code Playgroud)

Repro: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>
Run Code Online (Sandbox Code Playgroud)

调节器

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Sho*_*oxy 8

我最近得到了几乎相同的例外.我说几乎是因为他们实际上将异常重命名为Unexpected end of Stream, the content may have already been read by another component.,这实际上意味着某些东西已经消耗了身体流.以下更改的评论使我们了解正在发生的事情:

Tratcher于3月23日发表评论

... MVC模型绑定器读取表单并为您缓冲多部分段,因此使用MultipartReader重新解析请求体是没有意义的...

那么,问题是如何禁用默认表单绑定(读取请求表单)?

我发现DisableFormValueModelBindingAttribute这个Mvc.FileUpload示例中的属性 禁用了表单绑定,这就是它的样子:

[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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多信息,可以查看以下内容:

仅供参考 - 如前所述,MVC模型绑定器读取表单,但在哪里可以找到结果.结果可以在HttpRequest.Form,其中找到Files.


M-S*_*ons 7

不知道这可能对你有帮助,但我遇到了类似的问题"流的意外结束,内容可能已被其他组件读取".

app.Use(async (context, next) => {
            context.Request.EnableRewind();
            await next();
        });
Run Code Online (Sandbox Code Playgroud)

上面的代码在Startup.cs配置方法中添加.

希望能帮助到你

  • @programmerj 是的,我想出了这一点。然而,将其放入 Startup.cs 配置方法中会导致问题。最后,我设法通过创建自定义属性 public class EnableBufferingAttribute : Attribute, IResourceFilter {public void OnResourceExecuting(ResourceExecutingContext context) { context.HttpContext.Request.EnableBuffering(); 使其工作。} (2认同)