this is my code:
//code (CS CODE)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=.;database=chandu;integrated security=true");
String fn;
Int32 cnt;
Int32 i;
String pth;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_submit_Click(object sender, EventArgs e)
{
con.Open();
// creating folder dynamically on server
if (Directory.Exists(Server.MapPath(("~/Profile") + "/" + txt_foldername.Text)))//I HAVE CREATED ONE FOLDER TO MY PROJECT "PROFILE" INTO THIS FOLDER ALL NEWLY INSERTED FOLDERS WILL BE STORES
return; // if allready exists then it will not create it
else
// if not created then it will create it.
Directory.CreateDirectory(Server.MapPath(("~/Profile") + "/" + txt_foldername.Text));//I HAVE CREATED ONE FOLDER TO MY PROJECT "PROFILE" INTO THIS FOLDER ALL NEWLY INSERTED FOLDERS WILL BE STORES
SqlCommand cmd = new SqlCommand(" insert into adio_1(eid) values(@txt_foldername)",con );
//VALUES "+" (@ImgName,@ImgSize,@ImgPath)
con.Close();
}
protected void btn_fileupload_Click(object sender, EventArgs e)
{
String save; // storing the path of the image filename
// if image file size is greater then below condition will execute it.
if (FileUpload1.PostedFile.ContentLength > 0)
{
fn = Path.GetFileName(FileUpload1.FileName); // here fn will store the image filename
save = Server.MapPath(("~/Profile") + "/" + txt_foldername.Text) + "/" + fn; // making the path with created dynamically folder name
FileUpload1.SaveAs(save); // will save the image inside the dynamically created folder name
//Get Filename from fileupload control
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Getting dbconnection from web.config connectionstring
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
//Open the database connection
con.Open();
//Query to insert images path and name into database
SqlCommand cmd = new SqlCommand("Insert into adio_1(eid,ImageName,ImagePath) values(@eid,@ImageName,@ImagePath)", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@eid", SqlDbType.VarChar).Value=txt_foldername.Text;
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", Server.MapPath(("~/Profile") + "/" + txt_foldername.Text) + "/" + fn + filename);
cmd.ExecuteNonQuery();
//Close dbconnection
con.Close();
Int32 cnt;
Int32 i;
cnt = ListBox1.Items.Count; // will count the total collections in listbox
if (cnt == 0) // if no item is added in the listbox then it will save it and exit (return) from the code.
{ ListBox1.Items.Add(fn); return; }
//if listbox item is not empty the following code will execute.
for (i = 0; i <= cnt - 1; i++)
{
// check the filename if it is already exists in the listbox then it will not add it in.
if (ListBox1.Items[i].Text == fn)
{
Response.Write("file name already exists");
return;
}
}
// if not exists in the listbox then it will add it in listbox
ListBox1.Items.Add(fn);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cnt = ListBox1.Items.Count; // count the items in listbox
for (i = 0; i <= cnt - 1; i++)
{
if (ListBox1.Items[i].Selected==true)
{ //will delete the image from the dynamically created folder.
pth = (Server.MapPath(("~/Profile") + "/" + txt_foldername.Text)) + "/" + ListBox1.Text;
File.Delete(pth); //deleting the file from server
ListBox1.Items.Remove(ListBox1.Text); //removing the selected item in listbox.
return;
}
}
}
}
<%--designcode--%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" runat="server" class=”style1″>
<tr>
<td>
Enter Folder name
</td>
<td>
<asp:TextBox ID="txt_foldername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btn_submit" runat="server" Text="Create Folder"
OnClick="btn_submit_Click" />
</td>
</tr>
<tr>
<td>
Select Image</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Invalid File!(only .gif, .jpg, .jpeg, .wav Files are supported)"
ValidationExpression="^.+(.jpg|.JPG|.gif|.GIF|.jpeg|JPEG|.mp3)$"
ControlToValidate="FileUpload1"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btn_fileupload" runat="server" Text="Upload Image"
onclick="btn_fileupload_Click" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ListBox ID="ListBox1" runat="server">
</asp:ListBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Delete Selected Image" />
</td>
</tr>
</table>
<br />
<br />
<br />
</div>
</form>
</body>
</html>