How would you form your parameters for the action method which is supposed to receive one file
and one text
value from the request?
I tried this
public string Post([FromBody]string name, [FromBody]IFormFile imageFile)
And tried hitting it from Postman but it gives me 500 Internal Server Error
. I also can't debug it because it never hits the first statement inside the action method where I've put my breakpoint.
Any idea how can we parse boundary based requests and extract file(s) and other textual field(s)? I am new to ASP.NET Core.
I had the similar issue and I solved the problem by using [FromForm]
attribute and FileUploadModelView
in the function as follow:
[HttpPost]
[Route("upload")]
public async Task<IActionResult> Upload([FromForm] FileUploadViewModel model, [FromForm] string member_code)
{
var file = model.File;
// ...
}
This is a quick solution for anyone who is facing the same issue:
You will use ajax to send the following formData
let formData: FormData;
formData = new FormData();
formData.append('imageFile', imageFile);
formData.append('name', name);
Then you will receive it in your controller like this:
public string Post(IFormCollection data, IFormFile imageFile)
Then you will access the data as you do normally:
var name = data["name"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With