Hi
Just Look At This Code .
Copy and and paste it , and see how it works . You May Get An Idea With . This Example Works Fine
This is Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table 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" />
</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" AutoPostBack="True" Height="105px"
onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="109px">
</asp:ListBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Image ID="Image1" runat="server" Height="156px" Width="202px" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Delete Selected Image" />
</td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>
This is the .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;
public partial class _Default : System.Web.UI.Page
{
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)
{
// creating folder dynamically on server
if (Directory.Exists(Server.MapPath(txt_foldername.Text)))
return; // if allready exists then it will not create it
else
// if not created then it will create it.
Directory.CreateDirectory(Server.MapPath(txt_foldername.Text));
}
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(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
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(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;
}
}
}
protected void ListBox1_SelectedIndexChanged(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 display the selected image
Image1.ImageUrl = txt_foldername.Text + "/" + ListBox1.Items[i].Text;
}}}}
Thanks,
Tamilarasu
Work Hard To Work Good !... .. ...
Jerome, if this helps please login to Mark As Answer. | Alert Moderator