title: IFormFile copy to memorystream ObjectDisposedException
body: <p>I'm currently using tinyMCE to create some news post for a website.
  I need to be able to upload images, however i've hit a stopblock.</p>
  
  <p>When I hit my controller I get an </p>
  
  <pre><code>System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'FileBufferingReadStream'.
     at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ThrowIfDisposed()
     at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.set_Position(Int64 value)
     at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream..ctor(Stream inner, Int64 offset, Int64 length)
     at Microsoft.AspNetCore.Http.Internal.FormFile.OpenReadStream()
     at Microsoft.AspNetCore.Http.Internal.FormFile.CopyToAsync(Stream target, CancellationToken cancellationToken)
     at HardwareOnlineDk.Web.Areas.Admin.Controllers.ImageController.Upload(IFormFile inputFile) in D:\Kode\HardwareOnlineRider\HOL\SourceCode\Main\Web\Areas\Admin\Controllers\ImageController.cs:line 139
  </code></pre>
  
  <p>My code looks like this:</p>
  
  <pre><code>[HttpPost, ActionName("Upload")]
          public async Task&lt;IActionResult&gt; Upload(IFormFile inputFile)
          {
              try
              {
                  var filesCount = Request.Form.Files.Count;
  
                  if (filesCount == 0)
                      return BadRequest("Ingen fil fundet");
  
                  // Get HTTP posted file based on the fieldname. 
                  var file = Request.Form.Files.GetFile("file");
  
                  if (file == null)
                      return BadRequest("Fejlkonfiguration: Filnavn ikke korrekt");
  
                  // Check if the file is valid.
                  if (!Check(file.FileName, file.ContentType))
                      return BadRequest("Fil ikke gyldig");
  
                  var memoryStream = new MemoryStream();
                  await file.CopyToAsync(memoryStream);
                  var medie = new Medie
                  {
                      Name = file.FileName.Trim('\"'),
                      ParentId = _imageService.TempFolderGuid,
                      ContentLength = file.Length,
                      Content = memoryStream.ToArray()
                  };
  
                  try
                  {
                      var imageId = await _imageService.Medier_InsertMedie(medie);
  
                      //TODO Her skal vi gemme ImageId i Session
  
  
                      return Json(new
                      {
                          location = $"/api/media/{imageId.Id}.jpg"
                      });
                  }
                  catch
                  {
                      return BadRequest("Kunne ikke gemme billede");
                  }
              }
              catch
              {
                  return StatusCode(500);
              }
          }
  </code></pre>
  
  <p>And the Check Method if needed</p>
  
  <pre><code> private static bool Check(string filePath, string mimeType)
          {
              return AllowedImageExts.Contains(GetFileExtension(filePath)) &amp;&amp;
                     AllowedImageMimetypes.Contains(mimeType.ToLower());
          }
  </code></pre>
  
  <p>The code fails when i'm doing:</p>
  
  <pre><code>await file.CopyToAsync(memoryStream)
  </code></pre>
  
  <p>Can anyone help me here. I'm lost.</p>
  
  <p><strong>UPDATE 1</strong>
  I just tried to fix it with the suggested answer, so my code now looks like this:</p>
  
  <pre><code>[HttpPost, ActionName("Upload")]
          public async Task&lt;IActionResult&gt; Upload([FromForm]IFormFile file)
          {
              try
              {
                  if (file == null)
                      return BadRequest("Fejlkonfiguration: Filnavn ikke korrekt");
  
                  var memoryStream = new MemoryStream();
                  await file.CopyToAsync(memoryStream);
                  var filesCount = Request.Form.Files.Count;
                  if (!Request.ContentType.StartsWith(MultipartContentType))
                      return BadRequest("Contenttype er ikke korrekt");
  
                  if (filesCount == 0)
                      return BadRequest("Ingen fil fundet");
  
                  // Get HTTP posted file based on the fieldname. 
  
                  // Check if the file is valid.
                  if (!Check(file.FileName, file.ContentType))
                      return BadRequest("Fil ikke gyldig");
  
  
                  var medie = new Medie
                  {
                      Name = file.FileName.Trim('\"'),
                      ParentId = _imageService.TempFolderGuid,
                      ContentLength = file.Length,
                      Content = memoryStream.ToArray()
                  };
  
                  try
                  {
                      var imageId = await _imageService.Medier_InsertMedie(medie);
  
                      //TODO Her skal vi gemme ImageId i Session
  
  
                      return Json(new
                      {
                          location = $"/api/media/{imageId.Id}.jpg"
                      });
                  }
                  catch
                  {
                      return BadRequest("Kunne ikke gemme billede");
                  }
              }
              catch
              {
                  return StatusCode(500);
              }
          }
  </code></pre>
  
  <p>The input parameter is no longer null, but it still throws the same exception</p>