我正在尝试从https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
实现文件流示例作为其中的一部分,我已经实现了过滤器:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
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)
{
}
}
我收到了IOException
System.IO.IOException: Unexpected end of Stream, the content may have
already been read by another component.
at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.
<ReadAsync>d__36.MoveNext()
这太令人烦恼了。我发现了这个问题 Unexpected end of stream at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream
答案基本上是“实现DisableFormValueModelBinding属性”。显然,这是行不通的。
我的剃刀代码是
<form method="post" enctype="multipart/form-data" asp-controller="FileStore" asp-action="LargeUpload">
<div class="form-group">
<!--<div class="col-md-10">
<p>Please describe your file</p>
<input type="text" name="description"/>
</div>-->
<div class="col-md-10">
<p>Upload one or more files using this form:</p>
<input type="file" name="files" multiple/>
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Large Upload" />
</div>
</div>
</form>
我的控制器是:
[HttpPost]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LargeUpload()
{
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
{
return BadRequest($"Expected a multipart request, but got {Request.ContentType}");
}
//// Used to accumulate all the form url encoded key value pairs in the
//// request.
//var formAccumulator = new KeyValueAccumulator();
//string targetFilePath = null;
var boundary = MultipartRequestHelper.GetBoundary(
MediaTypeHeaderValue.Parse(Request.ContentType),
_defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
}
有人知道我是否错过了某些内容或教程是否过时了? 我正在使用ASP.NET Core 1.1.2运行VS2017社区