How to UPLOAD file with ASP.NET CORE WEB API | C# tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we will learn to upload files with asp.net core web api we will create an endpoint that can receive a file from a client and save the file to a blob storage my name is pat if you are new here consider subscribing because it motivates me to make more videos let's get into it let's create a new project in visual studio i select the asp.net core web api template i set the project name to web api file upload i disable https because i don't want to deal with certificates in my local environment i click on the create button to generate the project during the generation of the project visual studio created a weather forecast controller we won't use it so let's get rid of it let's also remove the model associated with the controller now we can start clean let's add a new controller we choose the api controller empty template the name is file upload controller let's add an action method called get this will be invoked when navigating to the api url the method returns a string which says file upload running api let's change the default url in the launch settings.json file we replace weather forecast with api file upload if you run the project you can see the file upload endpoint return the text correctly now let's add an endpoint for file upload i create a new action method called upload i decorate the method with the http post attribute because the endpoint will accept only http pass requests i decorate the method with another attribute called route to specify the route to the endpoint to retrieve the file from a http request i add a parameter of type iform file iform file represents a file sent in a http request the name of the parameter is file this needs to match the name sent in the http request as well the method returns ok as a result let's run the project now the api is running we are going to use passman to make a request in case you don't know postman is a tool that allows to test api i create a new request this needs to be a post request the endpoint is upload to send a file we are going in the body tab in the drop down list i select form data i select files in the key fill and name the key file the key must match the name of the parameter in the upload method in the value section i click on select files to select an image from my image folder once the image is selected i can submit the request to the api by clicking on the send button you can see that i hit the breakpoint we can inspect the properties of the file the upload works now let's save the file to azure blob storage i create a folder called service i add an interface called i storage service this interface exposes a method called upload which accept a form file as a parameter let's create a concrete implementation i call it storage service this class will need a package called azure.storage.blobs let's install it to interact with a blob storage we need an instance of blob service client class we are going to receive it through dependency injection we will configure layer let's implement the upload method get blob container client will give us a client to interact with the blob container we need to supply the container name in the parameter we will do it later get blob client on the container client will create a new blob in the container the blob will take the name of the submitted file upload on the blob client will write the stream read from the form file to the blob storage let's add storage settings to the app settings i add a new object name storage it has two properties the connection string and the container name in the storage service i use the configuration class to access the container name now i'll go into my azure account storage in the access key section i'll copy the connection string i paste it in the appsettings.json file let's also check if i have a container in which i can upload files yes indeed i have a files container let's update the name of the container in the app settings file the storage service is ready now let's configure dependency injection in the configure services method from the startup class i registered the i storage service with the storage service as its implementation to register blob service client class i'm going to add a package called microsoft.extensions.azure this package makes it easy to register azure services i use the add azure client method in the lambda expression i call add blob service client on the builder object i provide the connection string to the storage account now let's go back to the file upload controller we inject the storage service in the constructor in the upload method i call the upload method on the storage service to upload the file to the blob storage let's run the project the api is running in postman i'm going to use the same request i used before i get a 200 status which tells me the requests went ok let's check in the blob storage you can see the file has been uploaded i can download it that's the file i sent that's it for this tutorial if you enjoy like and subscribe thanks for watching see you soon
Info
Channel: Tech With Pat
Views: 41,102
Rating: undefined out of 5
Keywords: upload file asp.net core web api, file upload asp.net core, asp.net web api tutorial for beginners, file upload in asp.net core web api
Id: DrmwGNhrZ7c
Channel Id: undefined
Length: 7min 55sec (475 seconds)
Published: Wed May 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.