Updated on Kisan Patel
This tutorial will show you how to restrict the user to upload only specific type of files (.gif, .png or .jpg file) in ASP.NET?
Default.aspx
<p><asp:FileUpload runat="server" ID="FileUpload1" /></p>
<p><asp:Button ID="Button1" OnClick="UploadFile" runat="server" Text="Upload Specific Type of File" />
Default.aspx.cs
Namespace:
using System.IO.Path.GetExtension;
Code:
protected void UploadFile(object sender, EventArgs e) { string fileName = FileUpload1.PostedFile.FileName; string extension = Path.GetExtension(fileName); if (extension.Equals(".jpg") || extension.Equals(".gif") || extension.Equals(".jpg")) { string path = Server.MapPath("~/images/"); FileUpload1.SaveAs(path + fileName); Response.Write("File uploaded successfully !"); } else { Response.Write("upload only .jpg, .gif or .png file!"); } }