File Upload Control with File Type Filter

Johnbhatt
Posted by Johnbhatt under ASP.NET category on | Points: 40 | Views : 3127
Hi,

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

ASPX Code. We Placed a FileUpload Control and a button to Click.

<asp:FileUpload ID="FileUpload1" runat="server"  /> &nbsp; &nbsp;<asp:Button 
ID="BtnUpload" runat="server" Text="Upload File" onclick="BtnUpload_Click" />


Now BtnUpload_Click event in .cs page.
We are going to Upload only Picture (.JPG file or .BMP file).
    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;
}
}

Enjoy...

Comments or Responses

Login to post response