In this post i will going to show to check multiple file extension in c# while uploading the any document to server.
Step 1. Create the web page and add the following code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblWarning" runat="server"></asp:Label>
<asp:FileUpload ID="uploadFile_Id" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click"/>
<br /><br />
<asp:Image ID="imgId" runat="server" />
</form>
</body>
</html>
Step 2. Add the following code into cs file.
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
namespace Lab_1
{
public partial class FileExtension : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
imgId.ImageUrl = uploadFile(uploadFile_Id, "New_file_1");
}
private string uploadFile(FileUpload file, string fileName)
{
if (file.HasFile)
{
//Get the name of image
string FileName = file.PostedFile.FileName;
//Get the Extension of image
string Extension = Path.GetExtension(file.PostedFile.FileName);
string[] allowExtension = { ".doc", ".docx", ".ppt", ".pptx", ".png", ".xlsx", ".xls", ".pdf", ".jpg" };
if (allowExtension.Contains(Extension))
{
//Get The folder path from web.config file
string FolderPath = ConfigurationManager.AppSettings["folderPath"];
FolderPath += "/" + DateTime.Now.Second + "_" + fileName + Extension;
//Get the server path
string FilePath = Server.MapPath(FolderPath);
//Save image to uploader
file.SaveAs(FilePath);
lblWarning.Text = "";
return FolderPath;
}
else
{
lblWarning.Text = "Please upload valid file!";
//return "Extension Now Match";
}
}
return "";
}
}
}
Step 1. Create the web page and add the following code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblWarning" runat="server"></asp:Label>
<asp:FileUpload ID="uploadFile_Id" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click"/>
<br /><br />
<asp:Image ID="imgId" runat="server" />
</form>
</body>
</html>
Step 2. Add the following code into cs file.
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
namespace Lab_1
{
public partial class FileExtension : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
imgId.ImageUrl = uploadFile(uploadFile_Id, "New_file_1");
}
private string uploadFile(FileUpload file, string fileName)
{
if (file.HasFile)
{
//Get the name of image
string FileName = file.PostedFile.FileName;
//Get the Extension of image
string Extension = Path.GetExtension(file.PostedFile.FileName);
string[] allowExtension = { ".doc", ".docx", ".ppt", ".pptx", ".png", ".xlsx", ".xls", ".pdf", ".jpg" };
if (allowExtension.Contains(Extension))
{
//Get The folder path from web.config file
string FolderPath = ConfigurationManager.AppSettings["folderPath"];
FolderPath += "/" + DateTime.Now.Second + "_" + fileName + Extension;
//Get the server path
string FilePath = Server.MapPath(FolderPath);
//Save image to uploader
file.SaveAs(FilePath);
lblWarning.Text = "";
return FolderPath;
}
else
{
lblWarning.Text = "Please upload valid file!";
//return "Extension Now Match";
}
}
return "";
}
}
}