Bind Files in DropDownList from Server
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class banner : System.Web.UI.Page
{
protected string dirPath;
protected int dirPathLength;
protected String[] files;
public string strEmbed = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["bid"] != null)
{
getfilename();
}
}
}
public void getfilename()
{
ArrayList values = new ArrayList();
dirPath = Server.MapPath("../banners/");
dirPathLength = dirPath.Length;
dirPathLength = dirPathLength + 1;
files = Directory.GetFiles(dirPath, "*.*");
for (int i = 0; i < files.Length; i++)
{
string FileName = files[i];
FileName = FileName.Replace(Server.MapPath("../banners/"), "");
values.Add(FileName);
}
ddlselect.DataSource = values;
ddlselect.DataBind();
ddlselect.Items.Insert(0, "--Select--");
}
}
Validate File Upload using RegularExpressionValidator
GUI
<asp:FileUpload id="FileUploadFH" runat="server"></asp:FileUpload>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUploadFH"
Display="Dynamic" ErrorMessage="Only .pdf,.doc,.docx,.xls,.xlsx,.rtf are allowed" ValidationExpression="(.*?)\.(Pdf|doc|docx|xls|xlsx|rtf)$"></asp:RegularExpressionValidator>
Code Behind
if (FileUploadFH.HasFile)
{
FileUploadFH.SaveAs(Server.MapPath("../pdf/French/abc.doc") );
}