FTP在VB.NET中上传ProgressBar

时间:2013-04-12 15:35:13

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

我正在编写一个将文件上传到远程FTP服务器的应用程序。这是我的代码已经有效。

clsrequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

Dim bFile() As Byte = System.IO.File.ReadAllBytes(rutaorigen)

Dim clsStream As System.IO.Stream = clsrequest.GetRequestStream()

clsStream.Write(bFile, 0, bFile.Length)

clsStream.Close()
clsStream.Dispose()

现在我想在VB.NET中显示ProgressBar的进度。

文件不是太大(最多10 MB)。

我已经尝试了一个我在这里找到的例子,但它没有用。

我希望你能帮助我。谢谢!

2 个答案:

答案 0 :(得分:0)

控制台上的简单进展:

Dim request As WebRequest = WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.UploadFile

Using fileStream As Stream = File.OpenRead("C:\local\path\file.zip"),
      ftpStream As Stream = request.GetRequestStream()
    Dim buffer As Byte() = New Byte(10240 - 1) {}
    Dim read As Integer
    Do
        read = fileStream.Read(buffer, 0, buffer.Length)
        If read > 0 Then
            ftpStream.Write(buffer, 0, read)
            Console.WriteLine("Uploaded {0} bytes", fileStream.Position)
        End If
    Loop While read > 0
End Using

WinForms GUI进度:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Run Upload on background thread
    Task.Run((Sub() Upload()))
End Sub

Sub Upload()
    Dim request As WebRequest =
        WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
    request.Credentials = New NetworkCredential("username", "password")
    request.Method = WebRequestMethods.Ftp.UploadFile

    Using fileStream As Stream = File.OpenRead("C:\local\path\file.zip"),
          ftpStream As Stream = request.GetRequestStream()
        ProgressBar1.Invoke(Sub() ProgressBar1.Maximum = fileStream.Length)
        Dim buffer As Byte() = New Byte(10240 - 1) {}
        Dim read As Integer
        Do
            read = fileStream.Read(buffer, 0, buffer.Length)
            If read > 0 Then
                ftpStream.Write(buffer, 0, read)
                ProgressBar1.Invoke(Sub() ProgressBar1.Value = fileStream.Position)
            End If
        Loop While read > 0
    End Using
End Sub

enter image description here

答案 1 :(得分:-1)

我很久以前从一个例子中得到了这个。代码应该很容易根据您的需要进行更改。

Dim clsRequest As System.Net.FtpWebRequest = _
        DirectCast(System.Net.WebRequest.Create(ServLabel.Text & TextBox1.Text), System.Net.FtpWebRequest)

    clsRequest.Credentials = New System.Net.NetworkCredential(PassLabel.Text, UserLabel.Text)
    clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    rfshTMR.Enabled = True
    Dim File() As Byte = System.IO.File.ReadAllBytes(txtFile.Text)
    Dim clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()
    clsStream.Write(File, 0, File.Length)
    For offset As Integer = 0 To File.Length Step 1024
        ToolStripProgressBar1.Value = CType(offset * ToolStripProgressBar1.Maximum / File.Length, Integer)
        Dim chunkSize As Integer = File.Length - offset - 1
        If chunkSize > 1024 Then chunkSize = 1024
        clsStream.Write(File, offset, chunkSize)
        ToolStripProgressBar1.Value = ToolStripProgressBar1.Maximum
    Next
    clsStream.Close()
    clsStream.Dispose()
    MsgBox("File Is Now In Database", MsgBoxStyle.OkOnly, "Upload Complete")