上传数据时显示进度

时间:2011-01-25 13:51:01

标签: vb.net ftp progress-bar ftpwebrequest ftpwebresponse

我使用位于here的以下代码上传文件

Public Function UploadFile(ByVal oFile As FileInfo) As Boolean
Dim ftpRequest As FtpWebRequest
Dim ftpResponse As FtpWebResponse
Try
ftpRequest = CType(FtpWebRequest.Create(FTPSite + CurrentDirectory + oFile.Name), _
    FtpWebRequest)
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
ftpRequest.Credentials = New NetworkCredential(UserName, Password)
ftpRequest.KeepAlive = KeepAlive
ftpRequest.EnableSsl = UseSSL
If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = _
    New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length) As Byte
Using fr As FileStream = oFile.OpenRead
fr.Read(fileContents, 0, Convert.ToInt32(oFile.Length))
End Using
Using writer As Stream = ftpRequest.GetRequestStream
writer.Write(fileContents, 0, fileContents.Length)
End Using
ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
ftpResponse.Close()
ftpRequest = Nothing
Return True
Catch ex As WebException
Return False
End Try
End Function

我想扩展它,所以我也可以上传进度。 问题是我不知道从哪里开始。 显示上传进度的“逻辑”是什么?

在预定义的部分中“拆分文件”并上传它们或者什么?

2 个答案:

答案 0 :(得分:4)

您需要在后台线程上执行上传请求,以避免阻止UI线程。最简单的方法是使用BackgroundWorker类。它专为此类情况而设计。

    Dim backgroundWorker As New System.ComponentModel.BackgroundWorker()
    backgroundWorker.WorkerReportsProgress = True
    backgroundWorker.WorkerSupportsCancellation = True
    AddHandler backgroundWorker.DoWork, AddressOf Me.BackgroundFileDownload
    AddHandler backgroundWorker.ProgressChanged, AddressOf Me.ProgressChanged
    AddHandler backgroundWorker.RunWorkerCompleted, AddressOf Me.JobCompleted

事件ProgressChanged和RunWorkerCompleted在UI线程上运行,并允许您使用当前下载的状态相应地更新进度条。它们看起来像这样:

Protected Sub ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        progressBar.Value = e.ProgressPercentage
End Sub

在后台线程上调用DoWork,并且您想要实际调用您编写的UploadFile()函数。 对于FtpWebRequest,您将首先想要获取文件的大小,然后在上传数据块时,将您上传的内容除以文件的完整大小,以获得完整的百分比。像

这样的东西
Worker.ReportProgress(Math.Round((_bytesUploaded / _fileSize) * 100))

希望这有帮助。

答案 1 :(得分:1)

我不确定此代码是在Windows窗体还是网页上运行,这会对您实际显示进度的方式产生影响。但不管怎样,你首先要求这种方法报告它的进展情况。

要做到这一点,最好的办法是使用事件。以下是您需要添加到此类和函数中的内容:

首先是一个持有百分比的类:

Public Class ProgressEventArgs
    Inherits System.EventArgs

    Public Sub New(ByVal pPercentage As Decimal)
        _Percentage = pPercentage
    End Sub

    Private _Percentage As Decimal
    Public ReadOnly Property Percentage() As Decimal
        Get
            Return _Percentage
        End Get
    End Property

End Class

接下来,您需要向UpLoadFile所属的类添加一个事件:

Public Event Progress(ByVal sender As Object, ByRef e As ProgressEventArgs)

最后在UpLoadFile中,您需要提出此事件:

....
writer.Write(fileContents, 0, blockread) 
    RaiseEvent Progress(Me, new ProgressEventArgs(100 * block / blocks))
Next
....

无论你从哪里呼叫,你都可以听取这个事件:

Private Sub HandleProgress(ByVal sender As Object, ByRef e As ProgressEventArgs)
    '.... Update screen
End Sub

....
AddHandler YourUploadClass.Progress AddressOf HandleProgress