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

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.

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.

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:

The flow for the code behind page is as follows:

Download Source Files