Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
File Upload In ASP.NET
WhatsApp
Rameshyadav Medaboina
Jul 26
2016
2.5
k
0
0
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
Up Next
File Upload In ASP.NET