使用ASP.NET核心上传多部分文件会抛出IOException:Stream

时间:2017-09-04 08:41:58

标签: c# asp.net http asp.net-core

我正在尝试上传多部分文件,但在阅读Http请求的内容时,我一直收到以下异常

  

IOException:意外结束Stream,内容可能已被另一个组件读取。

我按照ASP.NET示例中项目中描述的示例,这是我的控制器方法的样子:

        [HttpPost("LoadMultipart")]
        [DisableFormValueModelBinding]
        public void UploadMultipartUsingReader()
        {
            string boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), DEFAULT_FORM_OPTIONS.MultipartBoundaryLengthLimit);
            MultipartReader reader = new MultipartReader(boundary, HttpContext.Request.Body);

            int totalBytes = 0;
            MultipartSection section;

            while ((section = reader.ReadNextSectionAsync().Result) != null) // This line causes the exception to be thrown
            {
                ContentDispositionHeaderValue contentDispo = section.GetContentDispositionHeader();

                if (contentDispo.IsFileDisposition())
                {
                    FileMultipartSection fileSection = section.AsFileSection();
                    int bufferSize = 32 * 1024;
                    totalBytes = ReadStream(fileSection.FileStream, bufferSize);
                }
                else if (contentDispo.IsFormDisposition())
                {
                    FormMultipartSection formSection = section.AsFormDataSection();
                    string value = formSection.GetValueAsync().Result;
                }
            }
         }

我已经开辟了大量的论坛和文档来解决这个问题,而且据我所知,出了什么问题,框架可能会自动将数据绑定到模型中,从而消耗了请求数据的流量,这就是由于[DisableFormValueModelBinding]标志应该禁用,所以我现在很困惑。

感谢您的任何见解

0 个答案:

没有答案
相关问题