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 - Network - Performing a File Upload using ASP.NET 2.0 and C# .NET
Performing a File Upload using ASP.NET 2.0 and C# .NET


ASP.NET Network Tutorial

This tutorial will show you how to perform a File Upload using the FileUpload control,.NET System.Text.RegularExpressions class, ASP.NET 2.0 and C#.NET

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

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

Looking for more ASP.NET Tutorials? Click Here!

The .NET Framework offers the FileUpload control to simplify uploading files from web pages.

For this example, we will need to first import the System.Text.RegularExpressions namespace. The System.Text.RegularExpressions namespace contains the classes we need to extract the filename of the file we wish to upload.

using System.Text.RegularExpressions;

We'll put our code in the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it first checks to see if the user selected a file. It then defines a pattern to extract the filename and extension. Once the pattern is matched it calls the fileUpEx.PostedFile.SaveAs() method with the complete path the file should be saved to In this case we are concatenating the Server.MapPath(".\") (current directory) and the filename.

protected void btnSubmit_Click(object sender, EventArgs e) {
if (fileUpEx.HasFile) {

string filepath = fileUpEx.PostedFile.FileName;
string pat = @"\\(?:.+)\\(.+)\.(.+)";
Regex r = new Regex(pat);
//run
Match m = r.Match(filepath);
string file_ext = m.Groups[2].Captures[0].ToString();
string filename = m.Groups[1].Captures[0].ToString();
string file = filename + "." + file_ext;

//save the file to the server
fileUpEx.PostedFile.SaveAs(Server.MapPath(".\\") + file);
lblStatus.Text = "File Saved to: " + Server.MapPath(".\\") + file;

}
}

We have the FileUpload control itself, a submit button and a label on the front end for user interaction. The front end .aspx page looks something like this:

<table>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> File to Upload:</td>
<td bgcolor="#FFFFFF">
<asp:FileUpload ID="fileUpEx" runat="server" /><br />
<asp:button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
<br />
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>
<br />

The flow for the code behind page is as follows:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (fileUpEx.HasFile) {

string filepath = fileUpEx.PostedFile.FileName;
string pat = @"\\(?:.+)\\(.+)\.(.+)";
Regex r = new Regex(pat);
//run
Match m = r.Match(filepath);
string file_ext = m.Groups[2].Captures[0].ToString();
string filename = m.Groups[1].Captures[0].ToString();
string file = filename + "." + file_ext;

//save the file to the server
fileUpEx.PostedFile.SaveAs(Server.MapPath(".\\") + file);
lblStatus.Text = "File Saved to: " + Server.MapPath(".\\") + file;

}
}

}


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

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

Looking for more ASP.NET Tutorials? Click Here!






 
  Developer Resources







Server Intellect Rocks