1

我正在使用位于此处的以下代码来上传文件

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

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

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

4

2 回答 2

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))

希望这可以帮助。

于 2011-02-02T19:56:58.280 回答
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
于 2011-02-02T18:24:18.440 回答