TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
File Upload In ASP.NET
Rameshyadav Medaboina
Jul 26
2016
Code
2.1
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
This is Source code,
<
form
id
=
"form1"
runat
=
"server"
>
<
b
>
selected file
</
b
>
<
asp:FileUpload
ID
=
"fileupload1"
runat
=
"server"
/>
<
br
/>
<
asp:Button
ID
=
"btnsubmit"
runat
=
"server"
Text
=
"upload"
OnClick
=
"btnsubmit_Click"
/>
<
br
/>
<
asp:Label
ID
=
"lblstatus"
runat
=
"server"
/>
</
form
>
The code view is,
protected
void
btnsubmit_Click(
object
sender, EventArgs e)
{
if
(fileupload1.HasFile) {
try
{
string
filetype = fileupload1.PostedFile.ContentType;
if
(filetype ==
"image/png"
|| filetype ==
"image/jpeg"
|| filetype ==
"image/jpg"
|| filetype ==
"image/gif"
|| filetype ==
"image/bmp"
) {
int
filelength = fileupload1.PostedFile.ContentLength;
if
(filelength <= 5242880) {
string
fname = Path.GetFileName(fileupload1.PostedFile.FileName);
string
sfolder = Server.MapPath(
"Ramesh//"
);
if
(!Directory.Exists(sfolder)) Directory.CreateDirectory(sfolder);
string
sfilepath = sfolder + fname;
fileupload1.SaveAs(sfilepath);
lblstatus.Text =
"<b style='color:green'>file uploaded successfullly!!!!!!!!"
+
"</b>"
;
}
else
{
lblstatus.Text =
"<b style='color:yellow'>please select image file size is max 5MB"
+
"</b>"
;
}
}
else
{
lblstatus.Text =
"<b style='color:deeppink'>please select only image files only......"
+
"</b>"
;
}
}
catch
(Exception ex) {
lblstatus.Text = ex.Message;
}
}
else
{
lblstatus.Text =
"<b style='color:red'>please select any one file"
+
"</b>"
;
}
}
ASP.NET
File Upload