【问题标题】:How do i add progressbar to track the upload of FTP如何添加进度条来跟踪 FTP 的上传
【发布时间】:2015-11-04 21:43:39
【问题描述】:

我目前正在研究可以同时发送大量文件的 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

【问题讨论】:

  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error... 但我做不到不太明白
  • @Plutonix 嘿,对不起,我忘了提,但是当我调试代码并上传文件时,它不会影响进度条值。
  • 您是否将WorkerReportsProgress 设置为true?
  • @Plutonix 是的,我试过了,但它没有改变任何东西跨度>
  • 您的进度从 0..1 而不是 0..100。乘以 100。

标签: .net vb.net ftp


【解决方案1】:

您可以不使用子 BackgroundWorker1_ProgressChanged,而是以某种方式获取上传速度(上传 1 KB、MB 等需要多长时间)并使用计时器来增加进度条使用信息。当程序启动时,只需让它上传一个小文件并记录上传需要多长时间。或者您可以在开始上传之前执行此操作。这样一来,您就不必依赖 BackgroundWorker1.ProgrssChanged 更改了,此外,您还可以实现估计的上传时间。

【讨论】:

  • 希望你意识到我的网速和别人不一样,所以记录上传是愚蠢的。
  • 怎么样?上传将在程序启动时开始,然后计时器可以记录在整个程序的其余部分中使用它作为变量所花费的时间
  • 好的,我会尝试制作一个(这个会下载而不是上传,但核心思想是一样的)我完成后会发布一个链接。
  • 好的,我完成了,这是链接:link 它没有进度条,但有可以在您的程序中使用的通用代码。点击 KBS 获取主文件的预计下载时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2013-05-19
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
相关资源
最近更新 更多