0

I was trying to do this on my own but have run into a situation where I need some help. I would like to use a progressbar control to show the current progress of an FTP file upload.

Currently, I am manually changing the value of the progressbar control - but I couldn't help but think that there's probably a better or simpler approach out there. It works right now, but the progressbar is sporadic in showing the progress based on the part of code which is being executed. Also, I attempted to throw the entire sub into a separate thread, but noticed that when I did that the progressbar doesn't show until the end of the code - then it flashes briefly and hides again.

Here's what I've done thus far, any help would be appreciated:

Public Sub uploadAuthorization()

    ProgressBar1.Show()
    Dim fileName As String = Path.GetFileName(TextBoxFilePath.Text)
    Dim ftpFolder As String = "authorizations"

    Try
        'Create FTP Request
        Me.Cursor = Cursors.WaitCursor
        Dim myRequest As FtpWebRequest = DirectCast(WebRequest.Create(ftpServer + "/" + ftpFolder + "/" + fileName), FtpWebRequest)
        ProgressBar1.Value = 20

        'Update properties
        myRequest.Credentials = New NetworkCredential(ftpUsername, ftpPassword)
        myRequest.Method = WebRequestMethods.Ftp.UploadFile
        ProgressBar1.Value = ProgressBar1.Value + 20

        'Read the file
        Dim myFile As Byte() = File.ReadAllBytes(TextBoxFilePath.Text)
        ProgressBar1.Value = ProgressBar1.Value + 20

        'Upload the file
        Dim myStream As Stream = myRequest.GetRequestStream()
        myStream.Write(myFile, 0, myFile.Length)
        ProgressBar1.Value = ProgressBar1.Value + 20

        'Cleanup
        myStream.Close()
        myStream.Dispose()
        ProgressBar1.Value = ProgressBar1.Value + 20

    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Information)
    End Try
    Me.Cursor = Cursors.Arrow
End Sub
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kismet Agbasi
  • 557
  • 2
  • 8
  • 28
  • You might be able to use WebClient.UpLoadFileAsync instead, e.g. http://www.vbforums.com/showthread.php?649866-WebClient.UploadFileAsync . You can add credentials: http://msdn.microsoft.com/en-us/library/system.net.webclient.credentials.aspx . – Andrew Morton Mar 03 '13 at 21:10
  • Thanks for the suggestion, I'll read up on it and let you know if I'm successful in implementing it or not. – Kismet Agbasi Mar 03 '13 at 23:05

1 Answers1

0

Hello again,

So based on suggested reading by Andrew Morton, here's what I came up with as a solution and it works like a charm. The only problem is that, the WebClient class does not support the UploadFileWithUniqueName method that the FtpWebRequest class offers. I really liked this - as it gave me the opportunity to use random file names, but I guess it's a fair trade-off to get the progress bar working.

So here's the solution:


Private WithEvents myFtpUploadWebClient As New WebClient

Private Sub ButtonChooseFile_Click(sender As System.Object, e As System.EventArgs) Handles ButtonChooseFile.Click

    If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        OpenFileDialog1.Title = "Please choose the Authorization File"
        TextBoxFilePath.Text = OpenFileDialog1.FileName
        ProgressBar1.Show()
        Me.Cursor = Cursors.WaitCursor

        Dim myUri As New Uri(ftpServer & OpenFileDialog1.SafeFileName)
        myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential(ftpUsername, ftpPassword)
        myFtpUploadWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileName)
    End If

End Sub

Private Sub myFtpUploadWebClient_UploadFileCompleted(sender As Object, e As System.Net.UploadFileCompletedEventArgs) Handles myFtpUploadWebClient.UploadFileCompleted

    If e.Error IsNot Nothing Then
        MessageBox.Show(e.Error.Message)
    Else
        Me.Cursor = Cursors.Default
        MessageBox.Show("Authorization Form Uploaded Successfully!")
    End If
End Sub

Private Sub myFtpUploadWebClient_UploadProgressChanged(sender As Object, e As System.Net.UploadProgressChangedEventArgs) Handles myFtpUploadWebClient.UploadProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
End Sub
Kismet Agbasi
  • 557
  • 2
  • 8
  • 28