ARTICLE

How to Upload Files in ASP.NET using VB.NET

Posted by John Arcadella Articles | ASP.NET using VB.NET January 18, 2011
How to Upload Files in ASP.NET using VB.NET
 
Reader Level:

ASP.NET 4.0 comes with a FileUpload control that allows you to send a file from your computer to a Web server. The file to be uploaded is submitted to the server as part of the browser request during postback. After the file has completed uploading, you can manage the file in your code.

1. Create an ASP.NET project in Visual Studio 2010.

2. Drag and drop a FileUpload control on Web Form.

3. Write this code on the Page Load event of the Web Form. You need to make sure your folder on the Web Server is set to a valid directory and have write permissions on it.

Protected Sub Page_Load(ByVal sender As Object,
       
ByVal e As System.EventArgs) Handles Me.Load
   
If IsPostBack Then
        Dim path As String = Server.MapPath("~/UploadFilesFolder/")
       
Dim fileOK As Boolean = False
        If FileUpload1.HasFile Then
            Dim fileExtension As String
            fileExtension = System.IO.Path. _
                GetExtension(FileUpload1.FileName).ToLower()
           
Dim allowedExtensions As String() = _
                {
".jpg", ".jpeg", ".png", ".gif"}
           
For i As Integer = 0 To allowedExtensions.Length - 1
               
If fileExtension = allowedExtensions(i) Then
                   fileOK = True
                End If
            Next
            If fileOK Then
                Try
                    FileUpload1.PostedFile.SaveAs(path & _
                         FileUpload1.FileName)
                    Label1.Text =
"File uploaded!"
                Catch ex As Exception
                    Label1.Text =
"File could not be uploaded."
                End Try
            Else
                Label1.Text = "Cannot accept files of this type."
            End If
        End If
    End If
End Sub

Login to add your contents and source code to this article
share this article :
post comment
 

ITS NOT WORKING........

Posted by sahil chhabra Dec 12, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor