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.
In HomeController.cs
In view - Index.cshtml
You can try this code.
Thanks!!
I'm using the following code to accomplish this in order to parse a response from Mailgun, which comprises both files and text values.
Please note that "dashifying" is just so property names like "MessageHeaders" get turned into "message-headers"; obviously you should use whatever logic makes sense for your use case.
Controller:
Model:
You Can Get Multi Images By Sample Code Through MultiPart.
First Inject IHttpContextAccessor To ConfigureService Method In Startup Class.Then Call It With Constructor Injection In Controller:
Action Method Can Have Response Or Not.
I guess you're transmitting the file and text in proper JSON format.
You should create a new class which includes file and text as properties of type string. Add the new class to your method arguments with attribute[FromBody] and you'll be able to parse the image string according to your needs.
Another way would be accessing the whole request content via
You would then have to parse the entire content, though.
I had the similar issue and I solved the problem by using [FromForm] attribute and FileUploadModelView in the function as follow:
This page helped me a lot https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
so now in my code I have the controller method as:
and a class for the model as
and the form like