Fileupload control in asp.net Part 30
C#, SQL Server, WCF, MVC and ASP .NET video tutorials for beginners
http://www.youtube.com/user/kudvenkat...
In this video we will discuss about fileupload control. FileUpload control is a combination of a text box and a browse button that enable users to select a file to upload to the server.
Closed Caption:
well welcome to redeem technologies and
blanket
this is part 30 of asp.net video series
in this session will 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 on to the web form
now remember a file upload control can
only be used to select the 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 on to the web form and let's
give this but in a meaningful name let's
call this upload file and let's give
this button an ID
ptn upload ok 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
ok so we have all the infrastructure and
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
ok 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 applaud you know
i am able to select the file that they
want to upload this test unless . do see
but then look at this upon selection
this will never get saved to the server
to upload that to the server and I have
to click this button and then we have to
write the code basically do that
ok but then before we do that we also
need to have several validations and
place for example let's say I want to
allow only word documents to be uploaded
onto the server and if the user doesn't
type or select anything here and then if
he 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 the file I don't want to
you know I don't want to use it to
upload a file greater than two MA and
this is very important to keep in mind
because these days
this denial of service attacks are you
know happening very frequently so too
wide them it's better you put a
restriction on the size of the file that
they can applaud
ok so along those lines let's say just
want to allow the user to upload only a
file that has got to mb will see you
have to do all that in a bed
ok now to upload the file it's actually
very simple
I have this file upload control it has
gotta save ask method
all you have to do is tell the part of
the server where you want to save this
file
ok now let's say in my asp.net web
application project i am going to have a
folder called uploads
ok 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 directly can
be anything and the which will directly
in the is can be different thing
now it respectful 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 part here like
sequel and backslash whatever you know
it's it's never a good practice that's
why in part in part 19 and 20 of this
video series we have actually discussed
about what's a virtual part and what's a
physical partners and how to map between
which will part and physical part using
this server . map part method
ok if you're new to that please watch
this video sessions before continuing
with the session
ok so I'm going to use that and then i'm
going to say so we're not map part and
then to this one
I'm going to tell you know tell the tale
is basically the root directory of this
web application project
so within the root directory of this web
application project find this upload
folder so uploads folder and written
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
named using the file upload controls
there's a property called file name
which gives you that that's all we're
done
ok and then probably in the label
control I want to display the message
that the file is uploaded so elder
message . text is equal to file upload
Edd and then maybe I want to board the
you know I want to change the color of
the label to green
so system.drawing that color . clean
that set
so when we run this now ok
there's an expected closing plant sis
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 in all the
log TX day 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.txt there we go
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 files look at that could
not find part of art 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 selected before he plays to
upload that how do we do that
it's very simple if file upload one .
there is a boolean property called has
file and if you look at that it's going
to return a true or false
if the file upload has file then only
upload as
display a message to the user lbl
message . text please select the file to
upload
ok and then maybe we want to change the
color to red
so system.drawing . kurla . read
so now if we run this and try to you
know upload a file without selecting the
file then we get a message because this
property returns fault so please select
the file to upload and if i select the
file and then try to upload that gets
uploaded fine
ok so another restriction is I want to
allow only word document in other files
which has got the daughter do see you .
do CX extension
so how do I do that ok so if the file if
the upload control has called the file
then what we want to do is we want to
check the extension so strength 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 the namespace called
system . i oh so you can either use the
using statement on the top or you can
use the fully qualified name here
so system that I oh there's a class part
part and within that class there is a
static method called get extension and
then if you give a full part to this
method is going to return you the
extension of that file numbers in that
part
so how do i get the file name here you
can use the file upload control . file
name
ok so you have the extension here now
f
if the file extension
now it's a good practice to actually
convert it to a lawyer case and then
compare it because if the users has
named the extension with a mix of upper
and lower case you want that to get
right so can wear all of them to lawyer
so if the file extension . to lawyer not
equal to . do say and if it's not equal
to . do CX extension if it's not any of
these then we know we have a problem
so we will just put a message saying
that
only files with dot DL C or D will see
ex extension are loved
ok and upload the file
so if it's one of those only then
perform this upload
ok but then another restriction that we
want ok so I'm okay if it's a word
document but again word document need to
have a limitation
I only want to allow the files which is
which are less than 2 mb
so the maximum size they can upload this
to mb ok so how do i check this size
okay so we know that the user has got
the file and the extension is for
document
ok but then before the upload again i
want to check the size
so how do i get the size let's say end
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 . uploaded file within
the uploaded
there's a property called posted file
which has caught a property called
content land
ok and if you look at the intelligence
here this gets the size of an uploaded
file and that we invite
ok but we want to calculate that in
megabytes ok
and we know that in a thousand 24 bytes
is one can abide and thousand twenty
four kilobytes is one megabyte
so if we want to get the total bytes
thousand 24 x thousand 24
that's one megabyte and that x 2 is two
megabytes to a 9 7 15 - okay so here we
have the file size F the file size is
greater than 29 7 15 -
then we know that the file size is
greater than to mba in which case we
don't want to do the upload
but will just display a message stating
that only files in off sighs to MBR
lawyer that allowed
maximum file size exceeded and maybe
maximum file size
um maybe want to specify that their
maximum file size 3 and be exceeded ok
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 moment
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 see your
daughter plz extension a love so that
validation is kicking again if I don't
select the file at all and try to upload
please select the 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 and see dr this
large file if you look at the size it's
2.5 7mb 2006 and 34 kb slightly greater
than 2mb 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 to M be
exceeded if you want you can also
compute
you can put that message saying that
your file sizes
whatever because we are able to
calculate their face file size there you
can print that to the user as well if
you want to but on the other hand if I
selective word document which is less
than 2mb here i have this test on the
list and if you look at that one before
we upload that test and list is only one
at phi k be well below to a me so i
should be able to upload that without
any problem
so upload the file uploaded successfully
and let's go to the web application
project
open this folder and Windows Explorer we
should see that test unless they're ok
so it's very simple to actually upload
files using this file upload control
with in asp.net and the important
properties here are a has file property
which returns it to our faults
indicating that whether of this file
upload control
has a file to upload or not and to get
the file extension use the get extension
method of system . i order part and then
the other important properties the
posted file and then the contentment
which gives you the total length of the
uploaded file on the slide you can find
resources for asp.net c shop and sequel
server interview questions
that's it for today thank you for
listening have a great day
Video Length: 14:17
Uploaded By: kudvenkat
View Count: 83,084