

Navigator : Home > Tutorials > Network Tutorials > ...
Performing a File Upload using ASP.NET 2.0 and C# .NET
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.
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
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:
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.
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;
} } } |
Looking for the VB .NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!