Sunday, 24 July 2011

Simple File Upload Control with ASP.NET and C#


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

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.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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();
}
}

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

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment