DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Performing a File Upload using ASP.NET 2.0 and VB .NET

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 migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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

Dim 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
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

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:

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

Dim 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
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class




Looking for the C# .NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!