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
您可能能够使用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
感谢您的建议,我会仔细阅读并告知您是否成功实施它。 – 2013-03-03 23:05:43