Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - File - Simple File Upload Control with ASP.NET and C#
Simple File Upload Control with ASP.NET and C#


ASP.NET File Tutorial

This tutorial will show how to implement the FileUpload control so that files can be uploaded to a website. C# version.


Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

In this tutorial, we will create both a single file upload form and a multi-file upload form.
After adding one FileUpload control for the single file upload and three for the multi-file upload, we two buttons to submit each form. We also add a GridView control to display the files which have been uploaded. Note the DataSource of this GridView.
The ASPX code looks something like this:

<form id="form1" runat="server">
<div>
<table style="width: 90%">
<tr>
<td style="width: 100px">
Single File Upload:<br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="buttonUpload" runat="server" Text="Upload" /><br />
<br />
Multi-File Upload:<br />
<asp:FileUpload ID="multiUpload1" runat="server" /><br />
<asp:FileUpload ID="multiUpload2" runat="server" /><br />
<asp:FileUpload ID="multiUpload3" runat="server" /><br />
<asp:Button ID="buttonMultiUpload" runat="server" Text="Upload" /></td>
<td style="width: 100px">
<asp:GridView ID="UploadedFiles" DataSource="<%# GetUploadList() %>" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>

The DataSource of the GridView is a function that collects the file list of the uploads folder, hence listing all the files in that folder in the GridView control.
Both of the Submit buttons run the same function -- UploadThisFile(). The only difference being, the multi-file upload button calls the function three times, each time with the different names of three FileUpload controls. And once the functions have been called, the new data is bound to the GridView, displaying the newly-uploaded files.
The code behind would look something like this:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;

partial class _Default : System.Web.UI.Page
{
override protected void OnInit(EventArgs e)
{
base.OnInit(e);

buttonUpload.Click += buttonUpload_Click;
buttonMultiUpload.Click += buttonMultiUpload_Click;
}

override protected void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
if (! IsPostBack)
{
UploadedFiles.DataBind();
}
}

protected string[] GetUploadList()
{
string folder = Server.MapPath("~/Uploads");
string[] files = Directory.GetFiles(folder);
string[] fileNames = new string[files.Length];
Array.Sort(files);

for (int i = 0; i < files.Length; i++)
{
fileNames[i] = Path.GetFileName(files[i]);
}

return fileNames;
}

protected void UploadThisFile(FileUpload upload)
{
if (upload.HasFile)
{
string theFileName = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
if (File.Exists(theFileName))
{
File.Delete(theFileName);
}
upload.SaveAs(theFileName);
}
}

protected void buttonUpload_Click(object sender, System.EventArgs e)
{
UploadThisFile(FileUpload1);
UploadedFiles.DataBind();
}

protected void buttonMultiUpload_Click(object sender, System.EventArgs e)
{
UploadThisFile(multiUpload1);
UploadThisFile(multiUpload2);
UploadThisFile(multiUpload3);
UploadedFiles.DataBind();
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks