如何添加进度条以跟踪FTP的上传

时间:2015-11-04 21:43:39

标签: .net vb.net ftp

我目前正在研究FTP类型的程序,它可以同时发送大量文件,我设法找到一些适合它的旧代码,我编辑了很多想法。无论如何我仍然有点学习,我试图让进度条工作,但我不能这样,我的代码有什么不对:

  Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim hInet As Int32 = InternetOpen("nn:", 1, vbNullString, vbNullString, 0)
    Dim hFtpSession As IntPtr = InternetConnect(hInet, "IPADDRESS", 21, "USERNAME", "PASSWORD", 1, 0, 0)
    For Each f As String In IO.Directory.GetFiles(FlatTextBox1.Text + "Files\")
        Dim fi As New IO.FileInfo(f)
        FtpPutFile(hFtpSession, f, fi.Name, 0, 0)
    Next
    Dim fileStream() As Byte = System.IO.File.ReadAllBytes(FlatTextBox1.Text + "Files\")
    Dim requestStream As System.IO.Stream = hInet.GetRequestStream() ' Here is my first issue.
    For offset As Integer = 0 To fileStream.Length Step 1024
        BackgroundWorker1.ReportProgress(CType(offset * ProgressBar1.Maximum / fileStream.Length, Integer))
        Dim chSize As Integer = fileStream.Length - offset
        If chSize > 1024 Then chSize = 1024
        requestStream.Write(fileStream, offset, chSize)
    Next
    InternetCloseHandle(hInet)
    FlatAlertBox1.Visible = True
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
End Sub

1 个答案:

答案 0 :(得分:-1)

而不是使用子 BackgroundWorker1_ProgressChanged ,您可以以某种方式获得上传速度(上传1千字节,兆字节等所需的时间)并使用计时器来增加进度条使用这些信息。程序启动时,只需上传一个小文件并记录上传所需的时间。或者你可以在开始上传之前完成。这样,您就不必依赖 BackgroundWorker1.ProgrssChanged 更改,而且您可以实现估计的上传时间。