September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

File Upload with VB.NET

  • July 17, 2007
  • By Irina Medvinskaya
  • Send Email »
  • More Articles »

Introduction

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.

Start Coding

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:

  1. Click on Toolbox, select HTML section, find the File Field control, and drag it onto the form.
  2. Right-click on the control and set "Run as Server Control".
  3. Change the Name property of the control to "File1".
  4. Click on Toolbox again, select Web Forms section, find a Button, and drag it onto the form.
  5. Set its Text property to "Upload" and its ID property to "cmdUpload".

Your screen should resemble this:



Click here for a larger image.

Add the following code to the form definition section of the .aspx file:

encType="multipart/form-data"

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:

Imports System.IO

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 here for a larger image.

Click Browse and choose a file to upload. Your screen should look like this:



Click here for a larger image.

Click Upload. You should see a message displayed on top:



Click here for a larger image.

You should check to see whether the file has been copied to the directory specified in the code with its original file name.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel