Click here to Skip to main content
15,884,298 members
Articles / Web Development / ASP.NET
Article

File Upload Manager

Rate me:
Please Sign up or sign in to vote.
3.26/5 (17 votes)
5 Mar 20053 min read 138K   3.7K   88   11
An article on how to upload files to a server and how to display the directories and files in a ASP.NET DataGrid

Sample Image - maximum width is 600 pixels

Introduction

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.

  1. <asp:TemplateColumn> which we use to display the check box.
  2. <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.
  3. this is also a boundcolumn. It stores the name of the file/directory.

The last three columns are <asp:TemplateColum>

  1. this column is used to represent the icon. ie, file or directory.
  2. used to display the name of the file or directory.
  3. used to display the size of the file.
ASP.NET
<asp:datagrid id="DataGrid1" runat="server" CssClass="griddatastyle" 
     Width="551px" AutoGenerateColumns="false" BackColor="#6699cc" 
     BorderColor="#cccccc">
<alternatingitemstyle CssClass="GridAlternatingDataStyle">
</alternatingitemstyle>
<pagerstyle ForeColor="#FFFFFF" BackColor="#6699CC"></pagerstyle>
<headerstyle HorizontalAlign="Left" Font-Size="8pt" ForeColor="White" 
             BackColor="#6699CC" Wrap="False"></headerstyle>
<itemstyle Font-Size="Smaller"></itemstyle>
<columns>
    <asp:templatecolumn ItemStyle-Width="1%">
        <itemtemplate>
            <asp:checkbox ID="chk" runat="server" />
        </itemtemplate>
    </asp:templatecolumn>
    
    <asp:boundcolumn Visible="false" DataField="Type" HeaderText="Type">
    </asp:boundcolumn>
    <asp:boundcolumn Visible="false" DataField="Name" HeaderText="HidName">
    </asp:boundcolumn>
        <asp:templatecolumn ItemStyle-Width="5%">
        <headertemplate>
        </headertemplate>
        <itemtemplate>
           <img src="<%# DataBinder.Eval(Container.DataItem,"FileIcon") %>"> 
        </itemtemplate>
    </asp:templatecolumn>
    
    <asp:templatecolumn ItemStyle-Width="50%">
        <headertemplate>
            Name
        </headertemplate>
        <itemtemplate>
          <asp:LinkButton id="LinkButton1" 
            CommandName='<%# DataBinder.Eval(Container.DataItem,"Type") %>' 
            CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Name")%>'
            Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>' 
            runat="server" >LinkButton</asp:LinkButton></td>
        </tecolumn>
    
    <asp:templatecolumn ItemStyle-Width="15%">
        <headertemplate>
            Size
        </headertemplate>
        <itemtemplate>
            <%# DataBinder.Eval(Container.DataItem,"Size") %>
        </itemtemplate>
    </asp:templatecolumn>
    
    <asp:boundcolumn HeaderText="Creation Time" DataField="CreationTime" 
                     SortExpression="CreationTime" ItemStyle-Width="15%" 
                          ItemStyle-Wrap="false"></asp:boundcolumn>

</columns>
</asp:datagrid>

The three 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.

C#
DataTable dt=new DataTable("datatable1");
DataColumn dc;
DataRow dr;
dr=dt.NewRow();

dc= new DataColumn();
dc.DataType=System.Type.GetType("System.String");
dc.ColumnName="Name";
dt.Columns.Add(dc);

dc= new DataColumn();
dc.DataType=System.Type.GetType("System.Int32");
dc.ColumnName="Size";
dt.Columns.Add(dc);

...

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.

C#
subdirectoryEntries
              = Directory.GetDirectories(Server.MapPath(".\\uploadfile\\"));
foreach(string directoryName in subdirectoryEntries)
{
                
    dr = dt.NewRow();
    DirectoryInfo di = new DirectoryInfo(directoryName);
                    
    dr[0] = di.Name;
    dr[1] = 1000; //(dirSize(di) / 1000) + 1;
    dr[2] = di.CreationTime;
    dr[3] = "D";
    dr[4] = "images/folder.gif";
    dt.Rows.Add(dr);
}

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.

C#
DataView dv = new DataView(dt);
DataGrid1.DataSource= dv;
DataGrid1.DataBind();

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.

C#
File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf(
                                                               "\\") + 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.

C#
Directory.Delete (Server.MapPath("\\uploadfile\\" + gridItem.Cells[2].Text),
                                                                    true);

if the selected item is file the follwing code is used.

C#
File.Delete (Server.MapPath("\\uploadfile\\" + gridItem.Cells[2].Text));

The "gridItem.Cells[2].Text" section gets us the name of the file/directory selected. Once we delete using the Delete button we refresh the grid using

FillGrid("",0);

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.

C#
DirectoryInfo df = Directory.CreateDirectory(
                              Server.MapPath("\\uploadfile\\")+dir_name);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks... Pin
chitmm25-Mar-10 17:19
chitmm25-Mar-10 17:19 
Generalexecutable Pin
rajeshndas16-Dec-09 12:42
rajeshndas16-Dec-09 12:42 
guys,
does anyone have an executable for this project that i can use?
Generalerror Pin
com494-Aug-09 1:44
com494-Aug-09 1:44 
Questionerror cs0260 Pin
miteshsolanki198426-Sep-08 1:49
miteshsolanki198426-Sep-08 1:49 
QuestionDisplay image as per the file extension Pin
Pushkar Joshi17-Oct-05 21:47
Pushkar Joshi17-Oct-05 21:47 
GeneralSome issues Pin
Robert Rohde5-Mar-05 21:44
Robert Rohde5-Mar-05 21:44 
GeneralRe: Some issues Pin
Anonymous20-Apr-05 23:50
Anonymous20-Apr-05 23:50 
GeneralRe: Some issues Pin
robertonai6-Dec-05 22:01
robertonai6-Dec-05 22:01 
GeneralRe: Some issues Pin
Jorge Oshiro20-Mar-06 13:37
Jorge Oshiro20-Mar-06 13:37 
GeneralRe: Some issues Pin
SilverHair201020-Apr-06 22:37
SilverHair201020-Apr-06 22:37 
GeneralRe: Some issues Pin
hotmanmax8-Dec-06 23:11
hotmanmax8-Dec-06 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.