Search code examples
c#asp.net-coremultipartform-data

C#: Posting Model from body and file as MultipartFormDataContent


I'm looking for a way to send model and image in one request. I tried to send my model from body, but I don't know how to send the file. Is there another way than sending image and model in a different files?

Here's the POST method from my API:

[HttpPost]
[Route("UploadNewEvent")]
public async Task<IActionResult> CreateNewEventAsync([FromBody] EventModel model)
{
    var file = this.Request.Form.Files.LastOrDefault();

    if (file != null)
    {
        var uploads = Path.Combine(_environment.WebRootPath, "uploads");

        using (var fileStream = new FileStream(Path.Combine(uploads, "test.jpg"), FileMode.Create))
        {
            await file.CopyToAsync(fileStream);
        }
     }

     // do sth with model later    
     return Ok();
}

And here's the code from my App:

public async Task SendNewEvent(EventModel model, MediaFile photo)
{
    var uri = $"{baseUri}api/User/Event/CreateNewEvent";

    if (photo != null)
    {
        var multipartContent = new MultipartFormDataContent();
        multipartContent.Add(new StreamContent(photo.GetStream()), "\"file\"", $"\"{photo.Path}\"");

        var httpClient = new HttpClient();               
        var jsonObject = JsonConvert.SerializeObject(model);
        var stringContent = new StringContent(jsonObject, Encoding.UTF8, "application/json");
        var httpResponseMessage = await httpClient.PostAsync(uri, stringContent);
    }
}

Solution

  • For passing Model with File parameter, you need to post data as form-data.

    Follow steps below:

    1. Change FromBody to FromForm

          [HttpPost]
      [Route("UploadNewEvent")]
      public async Task<IActionResult> CreateNewEventAsync([FromForm] EventModel model)
      {
          // do sth with model later    
          return Ok();
      }
      
    2. Change client code to send form-data instead of json string

          var uri = $"https://localhost:44339/UploadNewEvent";
          FileStream fileStream = new FileStream(@"filepath\T1.PNG", FileMode.Open);
          var multipartContent = new MultipartFormDataContent();
          multipartContent.Add(new StreamContent(fileStream), "\"file\"", @"filepath\T1.PNG");
      
      
          // EventModel other fields
          multipartContent.Add(new StringContent("2"), "Id");
          multipartContent.Add(new StringContent("Tom"), "Name");
          var httpClient = new HttpClient();
          var httpResponseMessage = httpClient.PostAsync(uri, multipartContent).Result;
      
    3. EventModel

      public class EventModel
      {
      public int Id { get; set; }
      public string Name { get; set; }
      public IFormFile File { get; set; }
      }