当我尝试从请求中读取多部分内容时,我得到一个异常,说明内容可能已被其他组件读取。
if (MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
{
// Used to accumulate all the form url encoded key value pairs in the
// request.
var formAccumulator = new KeyValueAccumulator();
var boundary = Request.GetMultipartBoundary();
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
while (section != null)
{
ContentDispositionHeaderValue contentDisposition;
var hasContentDispositionHeader =
ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);
}
}
答案 0 :(得分:8)
事实证明,我必须使用下面的属性禁用表单值模型绑定。
[HttpPost]
[Route("")]
[DisableFormValueModelBinding]
public async Task<IActionResult> Post()
属性实现在
之下[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
var factories = context.ValueProviderFactories;
factories.RemoveType<FormValueProviderFactory>();
factories.RemoveType<JQueryFormValueProviderFactory>();
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
答案 1 :(得分:6)
在 asp.net核心3 中,您必须将factories.RemoveType<FormFileValueProviderFactory>();
添加到DisableFormValueModelBindingAttribute
属性中。
代码
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
var factories = context.ValueProviderFactories;
factories.RemoveType<FormValueProviderFactory>();
factories.RemoveType<FormFileValueProviderFactory>();
factories.RemoveType<JQueryFormValueProviderFactory>();
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
答案 2 :(得分:1)
This为netcore 3.1为我工作
您需要添加
factories.RemoveType<FormFileValueProviderFactory>();
关于这种方法
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
var factories = context.ValueProviderFactories;
factories.RemoveType<FormValueProviderFactory>();
factories.RemoveType<FormFileValueProviderFactory>();
factories.RemoveType<JQueryFormValueProviderFactory>();
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
答案 3 :(得分:0)
我使用了[DisableFormValueModelBinding]
,但仍然出现错误,然后我还使用了:
if (Request.ContentLength == 0)
return BadRequest();
答案 4 :(得分:0)
public class OpenCSVReaderReadAllExample {
public static void main(String[] args) throws IOException {
CSVReader reader = new CSVReader(new FileReader("emps.csv"), ',');
List<String[]> records = reader.readAll();
Iterator<String[]> iterator = records.iterator();
while (iterator.hasNext()) {
String[] record = iterator.next();
System.out.print(record[0]);
System.out.print(record[0]);
}
reader.close();
}
}
中存在与此有关的错误。我将此软件包更新为Microsoft.AspNetCore.App 2.1.4
版本,并解决了。
答案 5 :(得分:0)
就我而言,这是“邮递员”问题。我已将文件发送到REST端点。到了某个时候,我开始出现此错误,但是代码没有更改。
我再次在“邮递员”中重新选择了此文件,一切正常。
答案 6 :(得分:0)
在我的特定情况下,我使用的是由MultipartReader解析的有效负载中不存在的多部分边界值:
// multipartBoundary is not in the contents of httpResponseContentStream.
var multipartBoundary = "batchresponse_63d0e4c7-4d49-4961-a294-3902d1730c44";
var multipartReader = new MultipartReader(multipartBoundary, httpResponseContentStream);
// Unexpected end-of-stream error used to occur here:
var multipartSection = await multipartReader.ReadNextSectionAsync(cancellationToken);
一旦对multipartBoundary进行了更新以反映要解析的内容中存在的实际边界值,该代码就会按预期工作。
答案 7 :(得分:0)
以防万一其他人正在追寻有关此错误的鬼魂: 使用 net5 web api 我有一个控制器
[HttpPost]
[Route("api/fileupload/{reportId}")]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> FileUpload(int reportId)
这不是个好主意。它给出了上述错误,我记得必须通过文件属性传递其他数据。那为我解决了。只是为了完整性。这不正常
[HttpPost]
[Route("api/fileupload")]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public async Task<IActionResult> FileUpload()