how to file upload and downlaod in .net core 8 web api

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's get start Demo First so this is same it is uh net core API with generic repository and unit work and here today we'll learn you know uh upload file multiple upload file and also how we can download that file so we will achieve this thing so let me check first let's see uh we can just you know try out and here the options like I can uh choose any file so whatever file I just choose as well I can ch I can pass it to our uh blog entity as well like for example chop and uh description whatever description and author name is karik so based on this actually once I execute it should be call and see once you uh file is created and uploaded in this uh resource whatever resource they will return return a path as well and with the file name so that we can store in our DB or anywhere and we can get it directly as well and same things I can do uh I can ret this file as well so let me show you the same API and here what I can do I will just change whatever name we are getting over here uh path name with the file name I will call directly so that I will show you that file actually store in our API and we can get back to over here so hit enter see so same file location also available in our API okay so that's are available here as well so same things I can do for this multiple upload and try to here like I can a to multiple file as well so for example I just import one then I can import another one and for example this and same things like angular and then whatever description and then author is Kik let me execute once you execute so it will return like object so if file name already exists so it will tell us okay it is already if it is not there then it will tell okay uh it it is given that resource file so this file already exist so what I will do let me add one more which is not exist so for example I hope this one is not exist so let me add and execute see uh two file is exist but last one is not exist that's why it's created also return back to over here and same things I can just check it like I if I pass that know path so I will I will see that uh file as well so let me show you like how looks like that file text file so if I change to that file name only here see the file also you able to see over here right with the data and one more end point I have created uh this session so that is called is download right so for example I want to download whatever file with the file name right so see this file I want to download so what I can do I just pass simply the file name once I execute it should be show over here as a download link you can just click over here you can just download directly or even whatever link they provided see the link provided like that so you can just call to that link and you can just download as well right so like this way uh you can download as well so not only that uh passing the swagar request we can get it if you want you can use it Postman as well so I'll show you as well Postman as well so see I already created this for the single file upload so what I'm trying to do over here let me show you I just use that file key whatever file key it is a file and then I just just use that file okay and here's the option actually so in this if you go to the postman there's a two option like if you want to text or file so you can just use that and uh don't worry about that this is demo so in the in the and end of the last uh I will show you uh what is how we can set this request and how we can get it okay similar you can just send it it is already exist that's why showing similar this multiple one so if you just send that request you will get that same whatever if it is exist so it will show that exist okay so uh so let's get start implementation so before implementation I'm especially recommend go to my channel and check out this series of net core B API with net 8 many many things actually I have covered in this uh Series so you can check out like generic repository generic repository with unit of work I know multi-v how can multi-v partial view uh know Auto mapper so many things actually I have covered also you can check out this over here see there's a many section actually so see there's expression also we have complete and uh if you go to generic model valat completed Global ex exception also handle Healthcare handle multiple DB product with know uh unit of work we have handling without unit of work also this handling over here right specification pattern also handling so many things actually we have covered so let's start implementation so I I just take same example uh which we have covered so far so this is that you know web API with net 8 and those are already Implement so what I will do simply I just create a new controller first right click on that create a new controller controller API controller add API controller and the call it to make it file controller name is file file controller that's it and once controller is ready what I need to do I need to uh create one model as well so that model I need to pass uh before model just create a you know signature of that method so it's simple I need to create like that see uh I just create a signature that uh endpoint so this is let me WRA this so this is simple signature um I create a public async task I action result and the method name is upload file and here this model actually will create just now we'll create actually a little bit later so this model will create it but yeah uh that endpoint name would be this sttp post and see this is another things actually if you check over here why I I add this attribute because if you not add this attribute that means only file allow 30 MB right so if you want to upload more than 30 so that that times you have to just specify if it is disabl request size or you can just specify size as well like whatever size you want so that also there's attribute you can set it so that's why I set it and finally I return back to return okay as have now just set it okay and whatever model will pass so let me later on we'll pass it here so that signature is ready now try to create that model class okay so there's a direct way I think uh we can can create that but uh I want to create this model inside our model folder so go to model folder where is my model folder data entity yes view model so go to that create a class and create this model folder and here simply you have to call you have to just create the model name property name right so my first property would be I form actually this will contain actually this is C this is sp. net core HTTP I form which is uh contain your whatever file request that will contain over here over the sttp request right so that's why I use I form okay and rest of property you can just keep it same whatever the name uh not like that file name but yeah I just whatever additional property if you want you can add it over here so I will add like actually uh I want to upload file along with my blog information so this is my blog information Blog name description and author so that is done and now uh go back to your controller here bring the name space so once you bring the name space now my first condition uh would be over here like check like if that file it's available or not right so it's simple uh just checking over here uh model dot uh file equal to null and model dot file length if it is length also suppose for example length also file length has a zero means Zero by so that also should not be proceed next right so what I will do simply I just return to bad request over here so this is my first condition and then uh I have to uh require some folder actually right over here so this is a normal mechanism I can store over here but yeah uh in real world we can store anywhere uh whatever file I'm getting but I'll show you because as of now I don't have a different server I have only this one local uh file structure in this API so that's why I will store over here so what I need to do I need to create one folder inside that folder I have to store all the files SI which will which we want to upload okay so what I will do uh simply uh let me create a folder folder name and just use that path dot combine and here I'll just ask I'll just put okay uh some whatever folder name you want so I just put it as a resource folder it should be resource folder and also inside the resource folder I have to create another folder that is called all files and whatever name you want you can keep it so I just create a two uh folder suppose this is a one folder inside this file uh all file folder should be inside this resource folder so that I just create a folder name as a resource resource and then what I will do next I just uh create a path to save where I want to save the folder right uh file right so that path also I have to need to create it so how we can create it simply again I will do use combine part do combine and this I will use another uh method directory. get directory so this get directory will get whatever your current directory right so suppose I'm using this B API so what would be my current directory my current directory would be over here right so that current directory location will be get over here and then whatever folder name I just you know combine like this resource inside this resource another folder so that I am sping specific pass over here so that it will combine another things like whatever current directory SL resource SL all folders so that will be created over here right so once it's created I have to check uh if the directory exist or not if it is not exist I have to create the new directory right so that actually show make sure like okay if that resource and all folder is director is available so that is fine if it is not there so let let me create for me right so that's how we can create it using the directory create directory method and pass it to path it will create automatically right so that is fine all right and now what I will do I have to just create uh store a file name so where I can get file name file name I can get from this model model had that's file property okay and this file property has actually many many uh many many fields right so if you go to I form let me go to here see there has a length name file name also content type and copy copy stream many things over here right so same property actually that property I can use go back to here okay so just store that file name as a store variable and then what I will do again I have to create a full path so let me create that full path so this is a full path how we can creating full path whatever our directory and know complete path where I want to save it okay that path along with the name so suppose my path is C and slash you know that resource and all folder then slash whatever file file name I have to specify over here so that that full path will be created over here where my file will be exist right so this is actually complete part and then what I will do I have to create a directory path okay means as a DB path mean whatever uh suppose resource on all files and then slash whatever file name so that path actually I need to get it to return back to this end point so that that you know that uh path they can why can use it and based on this they can store or we can store actually here so this is not important but yeah I just showing you right how we can get that DB path so that I can get the file as well so this is fine DB path is created now one more condition I will put over here what is that that is simple I will check if that whatever file file path is created right see whatever file like resource all file and slash whatever my name right let me show you the example suppose uh resource and then slash and suppose this is my directory path C and colon something something right and then resource and then all files and then my file name do e u whatever that extension I am not prevent anything but yeah uh jpg uh jpg and then suppose this this look if file already exist inside this folder so what I will do I just told okay this file already exist so how we can check it this one uh file do exist and passes that file path so this exist method will be check and it will tells if it is exist so it will share okay return back to bed request and it is not there then what I will do simply there's a m anyway actually retrieve that you know uh that stream whatever file we are trying to get over here in this file we can stream and uh stream here okay so using the string string and then file stream and we pass that full path over here and also you can just pass there's a many overload method so in this file is stream so we can see is a created and finally model. file there's a copy method over here I already show you inside this iform method iform U interface so you will get the copy so whatever stream you just pass this here it will trying to get and you know upload to that current location actually so whatever file path you giving over here so they will create an store in here okay there's another way also I will show you like not like this way there's a many any way actually we can uh write our file to our folder but this is first way I'll that I here but yeah in this next uh method where I can use that multiple files it is a single file actually supported but yeah if you want to send the multiple files also that will handle there I will use the another method all right so this is done now finally what I will do simply whatever that after success so I will return return here that path so this path actually we have created we have successfully uh uploaded our file right so that is done hopefully this should be work let me or run this and check there is a one issue uh what is that issue okay there's a many extra reses over here so let me run so go to that file see this is only one endpoint but just two endpoint also will be created uh just a bit later so try it and also I will put some debugger point over here go back to controller and so that I can show you what we are getting over here so first see this see uh I have to choose that file so let me choose one file and then pass it to like shop 12 whatever description and then also we can pass my name right and then execute once you execute you will get over here and let me show you see all the information I have whatever model property we get it as well as we are getting that file information right so this is file name this is a whatever content type and whatever you know that that things we are getting over here right so like this way uh we can just execute go back go go so so see directory is not exist that's why it's go here it will create a directory and also you can check see this is a directory we have just prepare so this director bars not exist so it's created and then file name be mentioned over here and also see the full path of the d project and this whatever our project is there so that project name be along this file name and that file also combine over here and then finally it will just create so once you continue uh definitely you able to see your file over here so I'll show you uh resource inside the resource all folder and see the file upload done right so this file already uploaded over here so this is way actually we can upload our file easily right with this along with the model and there's a you can just upload another way also like simply in instead of use that know form form uh attribute uh you can use directly that I form one right so there's a many way actually we can but this is a best way because I want to uh entity whatever entity along with the file name so that's why I use create a model and inside that model we have pass the property okay so move it to next uh end point okay let me stop this uh with support mult multiple uh multiple file uh will be support right so what I can do similar I have to just create another endpoint which is called post endpoint let me fix this so again I just use HTTP post multiple upload and here again uh just use that using this multiple DB create a method upload multiple upload file so multiple upload file just create change the signature and here I need to create for the another model for multiple multiple file upload right so similar model I have to create but that will support multiple so let me uh create a new model uh create a class and it's should be like multiple upload model right so let me create and let me copy from there file upload model because everything will be same only the multiple support right so let me go to there and here what I can do similar uh instead of this I can I can use the list and use this okay that's it right because in previously we use single file but yeah this iform can have that multiple support as well so that I can just modify that it and go back to your here and just copy this name and change it to over here that's it and still it is returning um something right okay so let me create a return type so that I can remove that error so that is done okay so now what I need to do I have to create I have to create you know similar implementation but it will be it's just for single one so that's why there's no issue but yeah for that multiple one I have to do something little bit different for that single one right so what I will do first I have to create one dictionary where I can store all the information like whatever file name and what is that you know uh what do you say uh like it is already it is it is created successfully so we can pass it to that uh the DB path right if it if it is created successfully so I said the dbpath if it is not successfully then me whatever message I can put over there as a message so that uh as a end point whatever response they can easily see and check okay if I pass Five file and three is uploaded to is fail so what is the fail reason so they can I know see and check according to there right it is a real world example actually so what I will do similar uh first I just create uh you know that model do files uh yeah one thing actually I miss uh if you go to multiple file over here so see this is not a single file right so that is why we have to put over here files and go back over here now it's gone right so yeah you can just put whatever name but yeah for the naming convention also should be correct over here so that is fine up to you and then what I will do I have to create one for each Loop over here because there is a collection right and this model has a collection of list of the collection So based on the collection suppose it have five so it should be TR five times so inside that I have to create same things whatever we have done right in for the single again I will create that path folder path and where I have to save that path okay and then what I will do again I have to just create that if it is path exist not then you have to create that like older path should be created and then similar again uh s same things we have to do just store as a file name then create a full path and then DB path also and the one last things we have to do over here in previously we have done using like this way right so now I will change uh to write our file to another way that's why I I told you right so there's a many way so first of all I will check if this file should not exist right uh so if should not exist I'm checking also if it is not exist then I have to create if it is if it is exist then I have to return something over here for example whatever our response I have to add like this right so this is is a dictionary I can add that our whatever file name along with our uh response message so I can put where if it is not exist then I need to write so how we can write again simply I have to create a memory stream over here like using where and like this way this is again is a very similar way and then what I will do I will use that whatever files that using copy to a string and then stor as a string a stream over here then we getting that stream right stream data of that file whatever file and then what I will do I have to just write to our location so there's another way I can use this system.io file. write all the bites async method here I can pass that whatever path I want to write and whatever memory stream is it required this method required actually see bytes right so how we can convert to bytes so whatever memory stream use that to array it will convert to bytes array right so once you done so this is way actually this is a very simple way and we can write our file inside this folder so that is done and finally uh what I will do whatever response I will return back to over here as a response that's it so let me run and check like as per expected it is be Behavior or not so go to see the new end pint already also created now try get now see here I can add multiple files right so choose for example this choose another one this choose another one is uh for example this and also just pass it to name whatever name and uh description and kic and execute once you execute now see response is null so that means some issue I guess over here but it is 200 but there is some issue what is the issue let me check or before that uh yeah there's a problem here actually I have to add as well after successful that response is fine because as of now uh whatever file I pass it nothing is exist so that's why all every everything is created I can show you see all the file has been created but uh our response is empty because we have not specified the response so what I need to do simply uh that response I can add like this this similarly uh the response. add whatever file name and whatever that uh resource you know path right so that I can specify over here just save it I hope the hot reloading is work working expected so it is also restarting now check one more time go to here try get add one 2 3 and also choose file one the file two and that file three and the similar name I can pass it over here author name execute now see this time whatever three files we have created uh just create right there's a no error but yeah same file when I try again execute see all the file already exist they will prompt us all the file already exist so this is also done and don't worry about that we will send as a request by a postman as well so once I complete all the end point we'll use the postman and the last and move it to the next end point which is now two is done which is uhoh which is download right how we can download this file so this is again uh concept of um you know file file system so let me create another endpoint that is called download and let me fix this so download and we are passing the name based on the name I can get that file uh download that file right so that name is there and again uh similar uh exercise we have to do we have to get some file name file path we are actually file path so those also we have to create it over here like that see I have just similar folder path where is our folder path we are we have to save actually whatever our saving path so it is just we can change it to know saving path uh store store file path also we can call it over here and then file name whatever name we are getting we are store opening here and then we are we have trying to get that full path so like this way we are trying to get that full path and then we can check that file is exist or Not So based on this pyth if it is not exist so we can tell okay the file is not exist if it is exist so what I will do again I will I have to read um the data see I'll just using await file read all whites with that whatever path okay so if it path exist So based on this path I will just read the read uh as a bytes okay this read method read all bytes method giving the bytes okay and this bytes what I will do simply I just return here the file contain okay I will use that see that file content which is actually um providing know file content result which will provide that file content and content type so here I will pass that bytes and along with this is type right so this is a application octet and stream type so you have to pass this and you can just file download name you can set it over here and then finally you can just return uh that file content result that's it so once it's done our all the API endpoint is ready right so if you go to over here see and whatever file name I have let me check uh so that I can show you as a download so go here suppose your K kk. jpg right so that I want to download so let me pass it over here ktic doj p g execute see once you click over here now if you open over here you will able to see my picture right so that is done so also we are able to see the download option right okay so I guess all the implementation done but one more thing actually I want to show you before moving to uh that Postman send the request uh as a as as I show as a starting I show the demo I can directly get that uh download path over here right for example what is that my uh Source folder it is a resource so let me get that folder name this is my folder name and also we have that all files and then my file name is kk.jpg right once I hit enter see Local Host cannot found because it is a static files right some files if you want to read your files from this you know B hosting uh your API hosting where is the hosting so what you need to do you have to do some additionally stuff you have to do so go to that program. file and here you have to set go to that you know not here this section you know after uh here uh you have to declare some middle we over here so what is that middleware app do use uh static files so this middleware you have to you have to use that and also additionally we have to do some addition additionally uh settings over here so that is setting is uh this right so use that static files and new static files option here this a file provider and request path so that it will allow and give it to know give it to allow to static files so that everybody can access directly via the URL so that's why we have to do this setting it is simply physically file provider whatever our root folder so I can give the access over here like like this way and I have to give that resources path over here as a string right so based on this once you save uh let me run again so now I'm able to access those folder so what is my folder my folder is uh resource and then slash uh what is my name all fold folders right so go to that all folders and then ktic dot jpg once you here now see now you're able to see access directly by the URL right so this will help actually once you uh you know Implement any UI so for example angular or any application so if you pass this directly URL with this you know uh this uh hosting endpoint along with this uh resource folder and file name so directly you can able to uh show that you know image on that page directly you don't need to do anything okay that's why I I'm showing uh I'll show you the implementation how we can do that so that is done and also we have okay one more thing you can do over here that related to file size so see the end point we are allowing that there is a disable request limit so that you can n number of size you can send it over here right so same things one more condition and know one more setting you have to do in this uh over here while you deploying this API okay on the server so there should not accept this request actually so what you can do you have to use this configuration on that like so that whatever request uh server will allow based on this configuration so you have to do this configuration like value length multiple body length and memory string right so like this way you can set it your things and one more issue I facing while I deploy this so that is actually you have to uh that is avoiding actually that aor um app services so if I send the more than 30 MB files though that that that uh request not accepted actually API is accepted but that hosting environment is not accepted so what you have to do you simp simply you have to call that you know bab do config file okay and there you have to add some configuration like the request body limit you can set that whatever request body limit you can add it like for example add new item that actually I'm showing you is additionally noce not here it if if you're using uh local implementation there should not be any issue but yeah once you deploy definitely this will be uh issue okay so see by default it is providing this okay so what you have to do you just uncomment and there's a more setting actually I don't have handy right now but yeah you if you search okay uh the increase the request in the we config so you will get similar things you can just pass it to over here that's it that I'm giving them some idea but yeah you can just sto create this file web.con in the root folder and you can publish before uh publish you can just you know create this file neither definitely you will get that issue okay so that is done now uh let me check out check uh for this here so what is my uh Endo my endpoint is this so I'll show you how we can send the request from the postman so open the postman go to here just change to this endpoint and base overl and the same endpoint actually we have created so I'm not changing what you need to do just choose the post and set it your URL and here go to the body if you go to body so see there's a none form data so you have to choose that form data because file data is uh upload file uh accept only this form data there's another way we'll come up with the next session we can use that know um here as a Json Json body while Json body how we can upload but here uh that will be a next session but here what you have to select from this option you have to choose that form body and here whatever file you know key like how we will get that key you have to go here and check out your model so this is actually key like whatever model you use so this is that key the file name description and author so those you have to add like same name if you change the name for example if you pass this so this will not will work it will say see the file field is required right because file is not match this name right so once I add this send it this so see request is bent over here you are able to see all the information and the continue so let me continue uh there's a some issue actually my system so that's why it's uh restarting but yeah uh run this application again go back over here and send request see it is 200 and also it is created right and once already created once you create again see file already exists okay so this setting you have to do it's very simple body form body and then you have to pass and you can just choose here uh this two option right file text and file so if you want to send a text so text also I think it should be support or not I haven't checked but yeah base 64 string I think we can use so that should be support but yeah there's a nice way actually you can just pass it to this select file and you can use any of file send so C file already exist right okay so move it to that multiple one also same things we have to do with the multiple only you have to change or not you have to change only this is my setting so I have to change this one and also multiple that is fine and similar you to be we have to do form data and here see there's a one more things you have to do taking care suppose you have multiple files right so I think some of the play we got uh that multiple options uh that is fine but here I don't have see any multiple options you know only one key have one value right so that's why I just created see there's a if you want to multiple files so same key you have to create multiple times okay and along with you have to set this file and file options and also you have to pass this so once you done so let me add see one file is exist and one file is not exist once you send again so two files already exist okay so that's all and uh yeah that's all for this session and the next session will come with you know uh that form body right Json form body how we can send that you know uh base 65 for string as of now we send as a HTTP request right but here how we can accept that form body as a Jon payload right so that I will do in the upcoming session okay so till now thank you have a nice day and don't forget to like subscribe and share
Info
Channel: DotNet Real world example
Views: 1,984
Rating: undefined out of 5
Keywords: file upload and download, .net core 8 web api, single file upload, multiple file upload, file download tutorial, asp.net core file handling, .net 8 coding example, web api development, how to upload file in .net core, how to implement file upload in .net core web api, how to download file in .net core web api, how to file upload and downlaod in .net core 8 web api, upload file in dot net core, file upload in .net
Id: d7Kad8Uadeg
Channel Id: undefined
Length: 43min 42sec (2622 seconds)
Published: Thu Feb 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.