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.
| using System.Text.RegularExpressions; |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
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; } |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
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> </table><td width="100" align="right" bgcolor="#eeeeee" class="header1"> File to Upload:</td> </tr><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> <br /> |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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; } } |
No comments:
Post a Comment