FileUpload Control Sample - ASP.NET

The following code shows how to implement file uploading in ASP.NET using the FileUpload control and an "Upload" button. (This code handles the Click event for the button.)

Protected Sub uxUploadButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    If uxFileUpload.HasFile Then

        Dim id As Integer = Session(Constants.SESSION_ID)
        Dim shortFileName As String = uxFileUpload.FileName

        Dim serverPath as String = Server.MapPath("~\uploads\TD\" & id.ToString())

        If Not Directory.Exists(serverPath) Then
            Directory.CreateDirectory(serverPath)
        End If

        Dim serverFile As String = Path.Combine(serverPath, shortFileName)

        uxFileUpload.SaveAs(serverFile)

    End If

End Sub