Pregunta

I'm coding an application that uploads a file to a remote FTP server. This is my code that already works.

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

Now I want to show the progress in a ProgressBar in VB.NET.

Files are not too big (10 MB max).

I've already tried an example that I found here, but it didn't work.

I hope you can help me. Thanks!

¿Fue útil?

Solución 2

I got this from an example a long time ago. The code should be fairly easy to change for your needs.

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

Otros consejos

Simple progress on console:

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 progress:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top