I want to create a method that uploads a file + takes an instance of a class\struct as an additional argument.
// Works
[HttpPost("test_1")]
public async Task<IActionResult> Test1(IFormFile file) { return Ok(); }
public struct MyModel
{
public int Value1 { get; set; }
public int Value2 { get; set; }
}
// Doesn't work
[HttpPost("test_2")]
public async Task<IActionResult> Test2(IFormFile file, MyModel model) { return Ok(); }
Calling test_2
produces following result:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "8000000c-0007-fd00-b63f-84710c7967bb"
}
How should I modify test_2
method to produce required result?
Because your request contains multi part (file and optional data).
So change it to form-data and you can get them with [FromForm] on Api.
Try this
public struct MyModel
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public IFormFile Files { get; set; }
}
[HttpPost("test_2")]
public async Task<IActionResult> Test2([FromForm]MyModel model) { return Ok(); }
Hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.