2
votes

I am trying to upload a file and also JSON body at the same time to a POST method as below

  public async Task<ResponseModel<PersonWriteResponse>> AddPerson([FromForm]IFormFile file, [FromForm]PersonPostRequest request)
        {
            var person = await _service.AddPerson(file,request);
            return ResponseModelHelper.BuildResponse(person, $"/production/person", "person");
        }

Both parameters are always null. In postman, I am specifying the content-type as "Multipart/form-data"
Is this the correct way of passing file and json data?

  • Alan-
1

1 Answers

-1
votes

I try to use model witch include IFormFile, and it's works

[HttpPost]
    [AllowAnonymous]
    public async Task<IActionResult> update([FromForm]MyFile model)
    {
        return Ok("Success!");
    }

    public class MyFile
    {
        public string Id { get; set; }        
        public IFormFile File { get; set; }
        // Other properties
    }

PostMan request here

You may set breakPoint at the return Ok("Success!"); line and saw what you get