This tutorial will show you how to perform a File Upload using ASP.NET 2.0, the FileUpload control, and VB .NET
The .NET Framework offers the FileUpload control to simplify uploading files from web pages.
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 Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click If Not fileUpEx.HasFile Is Nothing Then End SubDim filepath As String = fileUpEx.PostedFile.FileName Dim pat As String = "\\(?:.+)\\(.+)\.(.+)" Dim r As Regex = New Regex(pat) 'run Dim m As Match = r.Match(filepath) Dim file_ext As String = m.Groups(2).Captures(0).ToString() Dim filename As String = m.Groups(1).Captures(0).ToString() Dim file As String = filename & "." & file_ext 'save the file to the server fileUpEx.PostedFile.SaveAs(Server.MapPath(".\") & file) lblStatus.Text = "File Saved to: " & Server.MapPath(".\") & file End If Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
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 /> |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
The flow for the code behind page is as follows:| Partial Class _Default Inherits System.Web.UI.Page Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click If Not fileUpEx.HasFile Is Nothing Then End SubDim filepath As String = fileUpEx.PostedFile.FileName Dim pat As String = "\\(?:.+)\\(.+)\.(.+)" Dim r As Regex = New Regex(pat) 'run Dim m As Match = r.Match(filepath) Dim file_ext As String = m.Groups(2).Captures(0).ToString() Dim filename As String = m.Groups(1).Captures(0).ToString() Dim file As String = filename & "." & file_ext 'save the file to the server fileUpEx.PostedFile.SaveAs(Server.MapPath(".\") & file) lblStatus.Text = "File Saved to: " & Server.MapPath(".\") & file End If Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub |
No comments:
Post a Comment