Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Network - Performing a File Upload using ASP.NET 2.0 and VB .NET
Performing a File Upload using ASP.NET 2.0 and VB .NET


ASP.NET Network Tutorial

This tutorial will show you how to perform a File Upload using ASP.NET 2.0, the FileUpload control, and VB .NET

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

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

Looking for more ASP.NET Tutorials? Click Here!

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

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:

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


Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

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

Looking for more ASP.NET Tutorials? Click Here!






 
  Developer Resources







Server Intellect Rocks