File Upload with VB.NET
It has become a popular feature, often expected by users, to allow uploading documents into server-based applications. This is necessary in many applications, from very simple to more complex. In this article, I will show you a simple way of adding a File Upload feature into your ASP.NET applications created with VB.NET. Keep in mind that, although this article provides code samples in VB.NET, you could create the same functionality using C#, if that's your language of choice, by tweaking the code a little bit.
Users of various applications have become more sophisticated in utilizing the web. A number of systems, such as document management systems, content management systems, request management systems, and an array of others would require users to upload documents.
In the example, you will create a simple ASP.NET Web Application that will allow uploading a file to a server.
Open Visual Studio.NET and create a new Project with the following settings:
- Project Type: Visual C# Projects
- Templates: ASP.NET Web Application
- Location: http://localhost/FileUpload
On the web form:
- Click on Toolbox, select HTML section, find the File Field control, and drag it onto the form.
- Right-click on the control and set "Run as Server Control".
- Change the Name property of the control to "File1".
- Click on Toolbox again, select Web Forms section, find a Button, and drag it onto the form.
- Set its Text property to "Upload" and its ID property to "cmdUpload".
Your screen should resemble this:
Add the following code to the form definition section of the .aspx file:
As a result, the full form tag would look like this:
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
Add the following code to the declaration section of the .vb file:
Dim sFileDir As String = "C:\" Dim lMaxFileSize Long = 4096
Keep in mind that the values above will have to be modified to take into the consideration the specifics of your application. You also may make them dynamic and have your application read these values from either a database or an XML file.
Add the following code to very top portion of the .vb page:
Add the following procedure .vb code page:
Private Sub DeleteFile(ByVal strFileName As String) If strFileName.Trim().Length > 0 Then Dim fi As New FileInfo(strFileName) If (fi.Exists) Then 'if file exists, delete it fi.Delete() End If End If End Sub
Add the following code to the .vb file:
Private Sub cmdUpload_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdUpload.Click 'check that the file has been selected and it's a valid file If (Not File1.PostedFile Is Nothing) _ And (File1.PostedFile.ContentLength > 0) Then 'determine file name Dim sFileName As String = _ System.IO.Path.GetFileName(File1.PostedFile.FileName) Try If File1.PostedFile.ContentLength <= lMaxFileSize Then 'save file on disk File1.PostedFile.SaveAs(sFileDir + sFileName) lblMessage.Visible = True lblMessage.Text = "File: " + sFileDir + sFileName + _ " Uploaded Successfully" Else 'reject file lblMessage.Visible = True lblMessage.Text = "File Size if Over the Limit of " + _ lMaxFileSize End If Catch exc As Exception 'in case of an error lblMessage.Visible = True lblMessage.Text = "An Error Occured. Please Try Again!" DeleteFile(sFileDir + sFileName) End Try Else lblMessage.Visible = True lblMessage.Text = "Nothing to upload. Please Try Again!" End If End Sub
Press F5 to compile and run the project. Your screen should look like this:
Click Browse and choose a file to upload. Your screen should look like this:
Click Upload. You should see a message displayed on top:
You should check to see whether the file has been copied to the directory specified in the code with its original file name.