Uploading Files and Model data in one API call using FromForm Attribute in .NET Core Web API | LSC

preview_player
Показать описание
Hi Friends,

In this video title "Uploading Files and Model data in one API call using FromForm Attribute in .NET Core Web API ", we will see how to build a Web API in DOTNET CORE 6 to support file upload along with model data in the same API call.

The FromForm attribute in .NET Core Web API is used to handle the incoming HTTP requests that contain form data, including file uploads. With the FromForm attribute, you can easily extract data from the request and use it in your controller actions.

In your video, you demonstrate how to use the FromForm attribute to upload a file and other model data in a .NET Core Web API. You likely demonstrate how to create a controller action that is decorated with the FromForm attribute, and then show how to extract the form data from the incoming HTTP request.

You may also explain how to use the IFormFile interface to handle the uploaded file, and how to validate the incoming form data using data annotations or custom validation logic.

Overall, your video likely provides a step-by-step guide on how to use the FromForm attribute in .NET Core Web API to handle form data, including file uploads, and how to use this technique to build robust and scalable web applications.

Code:

Branch: feature/upload_files_with_model_example

Please post your questions in the comment section and I will be happy to answer your questions.

Happy coding!
Рекомендации по теме
Комментарии
Автор

Does the ProductImages map correctly?
I have a problem when using FromForm, where the array/list of objects is not mapped to its equivalent on C# side :/

drozdgabriel
Автор

Hi I just want to let you know that it does not work for me, maybe its because I am building a API and yours is not.

I have been digging into this and can confirm that if a request model have a property with IFormFile then [FromForm] should be use in the endpoints which will make the endpoints multipart so the same as as you suggested.

but the the request model also have properties of collections then testing from swagger will not work all collection will be 0

If you append it from the backend to the FormData.Collection[i] and iterate through it the backend will map it correctly.

but I want to be able to test from swagger UI that's always has been my first thing to do test endpoints.

So I ended up making all collection og type string instead, and then stringify the collection from the client, and deserialize the jsonstring in the backend - this works. If you can make it work with swagger let me know.

Best regards

oQu