title: How do I reduce the max size of a file upload for a multi part section in .net?
body: <p>I am using code from the .net samples on streaming file uploads:</p>
  <pre><code>    [HttpPost]
      public async Task&lt;IActionResult&gt; Post()
      {
          var request = HttpContext.Request;
  
          // validation of Content-Type
          // 1. first, it must be a form-data request
          // 2. a boundary should be found in the Content-Type
          if (!request.HasFormContentType ||
              !MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaTypeHeader) ||
              string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value))
          {
              return new UnsupportedMediaTypeResult();
          }
  
          var reader = new MultipartReader(mediaTypeHeader.Boundary.Value, request.Body);
          var section = await reader.ReadNextSectionAsync();
  
          // This sample try to get the first file from request and save it
          // Make changes according to your needs in actual use
          while (section != null)
          {
              var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition,
                  out var contentDisposition);
  
              if (hasContentDispositionHeader &amp;&amp; contentDisposition.DispositionType.Equals(&quot;form-data&quot;) &amp;&amp;
                  !string.IsNullOrEmpty(contentDisposition.FileName.Value))
              {
                  await _uploader.UploadAsync(section.Body);
                  return Ok();
              }
  
              section = await reader.ReadNextSectionAsync();
          }
  
          // If the code runs to this location, it means that no files have been saved
          return BadRequest(&quot;No files data in the request.&quot;);
      }
  </code></pre>
  <p>But the problem is I am able to upload a 20MB PDF. I want to reduce this to 5MB. Ideally I want to be able to set this per file extension.</p>
  <p>The documentation I have read suggests adding this to my Startup:</p>
  <pre><code>        var tenMB = 10485760;
          services.Configure&lt;FormOptions&gt;(options =&gt;
          {
              options.MultipartBodyLengthLimit = tenMB;
          });
  </code></pre>
  <p>As I understand this is supposed to throw an invalid data exception, but it doesnt. It doesnt do anything.</p>
  <p>What am I doing wrong here please? I dont think I can read the size of a stream without reading the stream into memory?</p>