title: Asp.Net Core API receiving empty IFormFile
body: <p>The last 2 days, I have been trying to create an image upload system for my website. When I try to save an uploaded image in the "wwwroot" of my Api, everything goes as planned except that I get an empty image in my folder. </p>
  
  <p>At the backend, I receive the filename I send in the frontend but the bytes of the image itself are not there. For some reason the the data of the stream I put in the post call is missing but I do receive the filename in the formfile. </p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>To clear things up about my application, I'm working with an Asp.Net Mvc as frontend and Asp.Net Api as backend. I know this isn't how you are supposed to use Asp.Net but this is a school project and I have to do it like this. Normally i would work with Angular or something else but that is not an option for me right now.</p>
  
  <p>So, I'm sending data from the Asp.Net Mvc (frontend) to the Asp.Net Api (backend) and I'm trying to do it by sending it as form data. That means there is no real form that is being submitted.</p>
  
  <p>This is the guide I tried to use:
  <a href="https://ilclubdellesei.blog/2018/02/14/how-to-upload-images-to-an-asp-net-core-rest-service-with-xamarin-forms/" rel="nofollow noreferrer">https://ilclubdellesei.blog/2018/02/14/how-to-upload-images-to-an-asp-net-core-rest-service-with-xamarin-forms/</a></p>
  
  <p><strong>Backend</strong></p>
  
  <p>ImageController:</p>
  
  <pre><code>[HttpPost("upload")]
  public async Task&lt;IActionResult&gt; UploadImage([FromForm(Name = "file")] IFormFile file)
  {
      if (file.Length == 0)
          return BadRequest("Empty file");
  
      string imageName = file.FileName;
      using (var fs = new FileStream("wwwroot/" + imageName, FileMode.Create, FileAccess.Write))
      {
          await file.CopyToAsync(fs);
      }
  
      return Ok();
  }
  </code></pre>
  
  <p><strong>Frontend</strong></p>
  
  <p>Method that uploads 1 image as a MemoryStream to the server</p>
  
  <pre><code>private async Task&lt;string&gt; Upload(Stream image, string name, string contentType)
  {
      _httpClient = _clientFactory.CreateClient("ProjectApi");
  
      HttpContent fileStreamContent = new StreamContent(image);
      fileStreamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") { Name = "file", FileName = name };
      fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
      using (var formData = new MultipartFormDataContent())
      {
          formData.Add(fileStreamContent);
          HttpResponseMessage response = await _httpClient.PostAsync("api/images/upload", formData);
          var input = await response.Content.ReadAsStringAsync();
          return input;
      }
  }
  </code></pre>
  
  <p>The content doesn't seem to be empty:</p>
  
  <p><a href="https://i.stack.imgur.com/kVS52.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kVS52.png" alt="enter image description here"></a></p>
  
  <p>The filename has been successfully send to the Api but the bytes of the image have not been send:</p>
  
  <p><a href="https://i.stack.imgur.com/UsE5Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UsE5Z.png" alt="enter image description here"></a></p>
  
  <p>Structure after uploading some images without checking the size of the formfile (They are empty):</p>
  
  <p><a href="https://i.stack.imgur.com/yq9lS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yq9lS.png" alt="The structure after uploading an image"></a></p>