I am looking to receive 1 CNCTask
class object from the body, but I also need to transfer a file in the same POST
request. I have written out my POST
route definition as:
[HttpPost("TaskUpload")]
[Consumes("application/json")]
public async Task<ActionResult<CNCTask>> Post([FromBody] CNCTask task, IFormFile file)
With my route defined as such, I just don't get how I will even transfer the file (e.g. when testing from Postman). A file would usually be sent in the body (I think ?), but if I already have my CNCTask
object being selected by the [FromBody]
, how do I negotiate this file transfer as well ?
I'm quite confused as to how to send both the class object and the file. Any tips would be appreciated.
CodePudding user response:
you have to create a ViewModel
public class ViewModel
{
public CNCTask Task {get; set;}
public IFormFile File {get; set;}
}
action
[HttpPost("TaskUpload")]
public async Task<ActionResult<CNCTask>> Post(ViewModel viewModel)
CodePudding user response:
You can retrive form data as follows
var formdata = Request.Form["formdata"];
var model = JsonConvert.DeserializeObject<ModelEntity>(formdata);
And write your controller this way
public async Task<ActionResult<CNCTask>> Post(IFormFile file)
{
var formdata = Request.Form["formdata"];
var model = JsonConvert.DeserializeObject<InitialDemandEntity>(formdata);
...
}