2

我正在编写一个将文件上传到远程 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()

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

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

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

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

4

2 回答 2

1

控制台上的简单进展:

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

在此处输入图像描述

于 2017-11-27T07:09:12.913 回答
-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")
于 2013-04-12T16:53:21.473 回答