Home ProgrammingASP.NET File Upload Control with File Type Filter

File Upload Control with File Type Filter

by John Bhatt
344 views

Hi,

We are going to learn how to upload a File using File Upload Control in ASP.NET.

ASPX Code

We Placed a File Upload Control and a button to Click.
[html]
[/html]

Back-end Code

Now BtnUpload_Click event in .cs page.
We are going to Upload only Picture (.JPG file or .BMP file).
[csharp]protected void BtnUpload_Click(object sender, EventArgs e)
{
string sSiteFolder = “ResumeFolder”;
string sRealPath = Server.MapPath(sSiteFolder);
if (!Directory.Exists(sRealPath))
Directory.CreateDirectory(sRealPath);
string CompletePath = sRealPath + FileUpload1.FileName;
string FileExt = CompletePath.Substring(CompletePath.IndexOf(“.”));
switch (FileExt)
{
case “.jpg”:
FileUpload1.SaveAs(sRealPath + FileUpload1.FileName);
break;
case “.JPG”:
FileUpload1.SaveAs(sRealPath + FileUpload1.FileName);
break;
case “.bmp”:
FileUpload1.SaveAs(sRealPath + FileUpload1.FileName);
break;
case “.BMP”:
FileUpload1.SaveAs(sRealPath + FileUpload1.FileName);
break;
}
}
[/csharp]
Enjoy…
John Bhatt

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

File Upload Control with File Type Filter

by John Bhatt time to read: 1 min
0