2013-03-03 32 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