2013-03-03 33 views
0

我試圖自己做到這一點,但遇到了我需要一些幫助的情況。我想使用進度條控件來顯示FTP文件上傳的當前進度。如何使用FtpWebRequest類的進度條?

目前,我手動更改進度條控件的值 - 但我忍不住想,可能有更好或更簡單的方法。它現在可以工作,但進度條在顯示基於正在執行的代碼部分的進度時是零星的。另外,我試圖把整個子程序放到一個單獨的線程中,但是注意到當我這樣做時,進度條直到代碼結束才顯示 - 然後它短暫閃爍並再次隱藏。

這裏是我迄今所做,任何幫助,將不勝感激:

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 
+0

您可能能夠使用WebClient.UpLoadFileAsync代替,例如http://www.vbforums.com/showthread.php?649866-WebClient.UploadFileAsync。您可以添加憑據:http://msdn.microsoft.com/en-us/library/system.net.webclient.credentials.aspx。 – 2013-03-03 21:10:37

+0

感謝您的建議,我會仔細閱讀並告知您是否成功實施它。 – 2013-03-03 23:05:43

回答

0

再次你好,

因此,基於由安德魯·莫頓提出的閱讀,這裏就是我想出了作爲解決方案,它的作用就像魅力。唯一的問題是,WebClient類不支持FtpWebRequest類提供的UploadFileWithUniqueName方法。我真的很喜歡這個 - 因爲它給了我使用隨機文件名的機會,但我想這是一個公平的取捨進度條的工作。

所以這裏的解決方案:


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