File Upload with specific file type

Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 1026
In apx page take label controls to display the message, File upload Control and Button
   <asp:Label ID="Label1" runat="server" ForeColor="Green"></asp:Label><asp:Label ID="Label2"
runat="server" Text="Label" ForeColor="Red"></asp:Label>
<asp:FileUpload ID="Fieldupload1" runat="server" />
<asp:Button ID="btn1" runat="server" Font-Bold="true" ForeColor="DodgerBlue" OnClick="Upload"
Text="Upload " />

In code Behind Under button click use this code,
 protected void Upload(object sender, EventArgs e)
string fileName = Fieldupload1.PostedFile.FileName;
string exe = Path.GetExtension(fileName);
if (exe.Equals(".gif") || exe.Equals(".jpg"))
string path = Server.MapPath("~/");

Fieldupload1.SaveAs(path + fileName);
Label1.Text = "File Uploaded successfully";

Label2.Text = "please Upload .gif or .jpg file";



Thanks & Regards

Comments or Responses

Login to post response