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