This code is used to upload files to the server, Create files on the server, display the files and directories in a Grid and to delete the files and directories that the user has selected.
I have coded this ASP.NET and C#.
Using the code
To use the article, just include the .aspx and .cs files to your project. You will also have to change the namespace to your namespace in the C# file. The Details of the code is as follows.
The main part in the .aspx file is the
ASP:DataGrid which acts as the user interface. The DataGird has six columns the function of each columns are as follows.
<asp:TemplateColumn> which we use to display the check box.
<asp:boundcolumn> is used to store a value which is either D or F. D means that the item in the row is a directory and F means that the item is a File.
- this is also a boundcolumn. It stores the name of the file/directory.
The last three columns are
- this column is used to represent the icon. ie, file or directory.
- used to display the name of the file or directory.
- used to display the size of the file.
<asp:datagrid id="DataGrid1" runat="server" CssClass="griddatastyle"
Width="551px" AutoGenerateColumns="false" BackColor="#6699cc"
<pagerstyle ForeColor="#FFFFFF" BackColor="#6699CC"></pagerstyle>
<headerstyle HorizontalAlign="Left" Font-Size="8pt" ForeColor="White"
<asp:checkbox ID="chk" runat="server" />
<asp:boundcolumn Visible="false" DataField="Type" HeaderText="Type">
<asp:boundcolumn Visible="false" DataField="Name" HeaderText="HidName">
<img src="<%# DataBinder.Eval(Container.DataItem,"FileIcon") %>">
ItemTemplates get the value from the
DataTable that we populate in the C# file as we will soon see.
We will now look into the C# code behind file.
The code in the DemoFileManager.aspx.cs is relatively simple. We first poplute the
DataGrid using the
FillGrid. The idea here is to first create a
DataTable dt=new DataTable("datatable1");
dc= new DataColumn();
dc= new DataColumn();
Note that we have to declare a
DataColumn, specify its DataType, its name and then add to the DataTable. Once the columns are specified, we use the
Directory.GetDirectories function to get the list of all the subdirectories inside the directory called "uploadfile", which i use as the base directory for upload.
foreach(string directoryName in subdirectoryEntries)
dr = dt.NewRow();
DirectoryInfo di = new DirectoryInfo(directoryName);
dr = di.Name;
dr = 1000; dr = di.CreationTime;
dr = "D";
dr = "images/folder.gif";
similarly we use
Directory.GetFiles(Server.MapPath(".\\uploadfile\\")) to get the files inside "uploadfile" directory and we add it to the table. Once all that is done we bind the DataTable to the DataGrid.
DataView dv = new DataView(dt);
The next section is the UpLoad File. This section is simple. We use the following code to browse and select the file. We then perform some simple
string operation to extract the name of the file and upload it to the server. I have not written the code to upload the file to directories other than the base directory.
"\\") + 1) ;
The Delete function checks if there is any checked checkbox. If the check box is checked, we check to see if the checked item is a directory or file and delete accordingly. if the selected item is Directory the following code is used.
Directory.Delete (Server.MapPath("\\uploadfile\\" + gridItem.Cells.Text),
if the selected item is file the follwing code is used.
File.Delete (Server.MapPath("\\uploadfile\\" + gridItem.Cells.Text));
The "gridItem.Cells.Text" section gets us the name of the file/directory selected. Once we delete using the Delete button we refresh the grid using
Entering a name in the text box and them clicking on the “Create New Directory” button creates a new director under the upload directory. This is the code to create the directory.
DirectoryInfo df = Directory.CreateDirectory(