Fileupload control in asp.net Part 30

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well welcome to prism technologies I am venket this is part 30 of asp.net video series in this session we'll learn about uploading files to the server using the asp.net file upload control before continuing with the session I strongly recommend to watch parts 19 and 20 of this video series let's drag and drop the file upload control onto the web form now remember a file upload control can only be used to select a file that they want to upload this control will not actually upload the file to upload the file let's drag and drop a button control onto the web form and let's give this button a meaningful name let's call this upload file and let's give this button an ID BTN upload okay to display the status of the upload let's drag and drop a label control let's flip to the source let's give this a meaningful ID LBL message let's get rid of the text property and let's set the font bold is equal to true okay so we have all the infrastructure in place to upload the file so when I click this upload file button that's when the file should actually be uploaded to the server okay so if we run the application as it stands right now you know I am I will be able to select the file that I want to upload you know I am able to select the file let's say I want to upload this test analyst or do C but then look at this upon selection this will never get saved to the server to upload that to the server you know you have to click this button and then we have to write the code to basically do that ok but then before we do that we also need to have several validations in place for example let's say I want to allow only Word documents to be uploaded on to the server and if the user doesn't type or select anything here and then if you tries to upload this file then I should get a validation message saying that you haven't selected a file to upload and along the same lines if you have selected a file I don't want to you know I don't want the user to upload a file greater than 2 MB and this is very important to keep in mind because these days this denial of service attacks are you know happening very frequently so to avoid them it's better you put a restriction on the size of the file that they can upload okay so along those lines let's say I just want to allow the user to upload only a file that has got 2 MB we'll see how to do all that in a bit okay now to upload the file it's actually very simple I have this file upload control it has got a save as method all you have to do is tell the part of the server where you want to save this file okay now let's say in my asp.net web application project I am going to have a folder called uploads okay so all uploads that the user does should be going into this folder ok so now look at this this web application project can be deployed on any server and the physical directory can be anything and the virtual directly in the ia s can be different things now irrespective of whatever is the physical directory where this project is present you know I want all the uploads to be going into this folder so for that reason it's not a good practice to actually hard code the path here like C colon backslash whatever you know it's never a good practice that's why in part in parts 19 and 20 of this video series we have actually discussed about what's a virtual path and what's a physical path is and how to map between virtual path and physical path using the server dot map path method ok if you're new to that please watch these video sessions before continuing with this session ok so I'm going to use that and then I'm going to say server dot map path and then to this one I'm going to tell you know till the till it's basically the root directory of this web application project so within the root directory of this web application project find this uploads folder so uploads folder and within that we want to save the file ok now I want to retain the name of the file that the client has used ok so how do I get the client files name using the file applaud controls there is a property called filename which gives you that that's all we are done okay and then probably in the label control I want to display the message that the file is uploaded so LBL the message dot txt is equal to file uploaded and then maybe I want to bold thee you know I want to change the color of the label to green so system the drawing dot color dot green that's it so when we run this now okay there's an expected closing parenthesis let's run this now so now I should be able to upload a file onto the server so I go there I select a file let's say I want to select you know the log txt and I upload look at that file uploaded and then if we go and open this folder in Windows Explorer you should see that log dot txt there very good ok so the file got uploaded now the first restriction if the user doesn't select look at that if I don't select a file and then if I click on upload file look at that could not find part of odd you know I am basically getting an exception there okay so we need to have validation in place we need to make sure that the user has a file selector before he tries to upload that how do we do that it's very simple if file upload one dot there is a boolean property called a hash file and if you look at that it's going to return a true or false if the file upload has filed then only upload as display a message to the user LBL message dot txt please select a file to upload okay and then maybe we want to change the color to red so system the drawing dot color dot red so now if we run this and try to you know upload a file without selecting a file then we get a message because this property returns false oh please select a file to upload and if I select a file and then try to upload that gets uploaded fine okay so another restriction is I want to allow only Word documents you know the files which has got the dotted do C or dot do C X extension so how do I do that okay so if the filer if the upload control has caught the file then what we want to do is we want to check the extension so string file extension so the next question is how do we get the file extension now anything that you want to do with files is actually present in a namespace called system dot IO so you can either use the using statement on the top or you can use the fully qualified name here so system dot IO there is a class called path and within that class there is a static method called get extension and then if you give a full path to this method it's going to return you the extension of that file name within that path so how do I get the file name here you can use the file upload control dot file name okay so you have the extension here now if if the file extension now it's a good practice to actually convert it to a lowercase and then compare it because if the users has named their extension with a mix of upper and lowercase you want that to get right so convert all of them to lower so if the file extension dot to lower not equal to dot D Orsay and if it's not equal to dot d or CX extension if it's not any of these then we know we have a problem so we will just output a message saying that only files with dot do C or dot d whoa CX extension are allowed okay else upload the file so if it's one of those only then perform this upload okay but then another restriction that we want okay so I'm okay if its word document but again word document need to have a limitation I only want to allow the files which is which are less than to the MB so the maximum size they can upload is 2 MB ok so how do I check the size ok so we know that the user has got the file and the extension is for document ok but then before they upload again I want to check the size so how do I get the size let's say nth file size is equal to so now we need to get the file size now to get the file size actually we have you know the file upload control dot uploaded file within the uploaded there is a property called posted file which has got a property called content land okay and if you look at the intellisense here this gets the size of an uploaded file and that too in bytes okay but we want to calculate that in megabytes okay and we know that in a thousand 24 bytes is one kilobyte and thousand twenty four kilobytes is one megabyte so if we want to get the total bytes thousand 24 multiplied by thousand 24 that's one megabyte and that multiplied by two is two megabytes - or nine seven one five - okay so here we have the file size F the file size is greater than two or nine seven one five two then we know that the file size is greater than 2 MB in which case we don't want to do the upload but we'll just display a message stating that only files you know of size 2 MB are lower are allowed maximum file size exceeded and maybe maximum file size - ma we want to specify that their maximum file size 2 MB exceeded okay so let's see if this works as expected so let's run this now so we have all the validations in place at the minute okay first of all let's try to upload you know an executable here I selected an executable and that's dangerous let's try to upload that look at that only files with dot do C or dot B or C extension or a lab so that validation is kicking in if I don't select a file at all and try to upload please select a file to upload on the other hand let's say I select a Word document but then I have a file here which is called large file and if you look at that that's actually in C Drive so if you look at that in C Drive this large file if you look at the size it's 2.5 7 mb 2634 kb slightly greater than 2 mb let's try to upload that and see what's going to happen so I select that upload file look at that maximum file size 2 mb exceeded if you want you can also compute you can output that message saying that your file size is whatever because we are able to calculate their Feist file size there you can print that to the user as well if you want to but on the other hand if I select a Word document which is less than 2 MB here I have this test analyst and if you look at that one before we upload that test and list is only 1 out of 5 KB well below 2 MB so I should be able to upload that without any problem so upload so file uploaded successfully let's go to the web application project open this folder and Windows Explorer we should see that test unless there ok so it's very simple to actually upload files using this file upload control within asp.net and the important properties here or a has file property which returns a true or false indicating that whether if this file upload control has a file to upload or not and to get the file extension use the gate extension method of system dot IO dot path and then the other important property is the posted file and then the content length which gives you the total length of the uploaded file on this slide you can find resources for asp.net c-sharp and sequel server interview questions that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 179,653
Rating: undefined out of 5
Keywords: fileupload, control, asp.net, c#, file, upload, asp net fileupload hasfile, asp.net fileupload check file extension, upload file to server asp net, c# file upload example, file upload server.mappath in asp.net, asp.net file upload control example c#, c#.net file upload example, file upload server side code c#, asp net fileupload check file size before upload, upload file from client to server asp.net c#, fileupload control no file chosen, upload file to server folder asp.net
Id: irF6Zomjxwc
Channel Id: undefined
Length: 14min 17sec (857 seconds)
Published: Fri Nov 16 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.