Jump to content

DownloadFileAsyn Downloads de vários ficheiros


HQuintas

Recommended Posts

Boas,

Quero fazer o download de vários ficheiros. Para resolver o problema coloquei um ciclo While client.IsBusy, para só avançar para o próximo quando estiver livre. Porque só pretendo que aplicação continue depois de ter feito todos os downloads. O que pretendo saber é se isto é o mais correcto ou se existe outra forma de o fazer. Obrigado.

 

Imports System.Net

Public Class frm_download

    Dim client As WebClient = New WebClient

    Private Sub Download_file()
        Dim fileReader As New WebClient()
        AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
        AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted

        client.UseDefaultCredentials = True
        client.Credentials = New NetworkCredential("user", "password")

        Dim files As New List(Of files_items)
        Dim file1 As New files_items
        Dim file2 As New files_items

        file1.origem = "http://helderquintas.16mb.com/pasta_protegida/www.txt"
        file1.destino = "xxx.txt"

        file2.origem = "http://helderquintas.16mb.com/pasta_protegida/www.txt"
        file2.destino = "www.txt"

        files.Add(file1)
        files.Add(file2)

        For Each a In files
            While client.IsBusy 'Enquanto estiver a fazer um download espera para fazer o próximo.
            End While
            client.DownloadFileAsync(New Uri(a.origem), a.destino)
        Next

    End Sub

    Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

        Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
        Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
        Dim percentage As Double = bytesIn / totalBytes * 100

        ProgressBar1.Value = Int32.Parse(Math.Truncate(percentage).ToString())

    End Sub

    Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        MessageBox.Show("Download Complete")
    End Sub

    Private Sub frm_update_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Download_file()
    End Sub

End Class

Public Class files_items
    Implements IEquatable(Of files_items)
    Public origem As String
    Public destino As String
    Public Function Equals1(other As files_items) As Boolean Implements IEquatable(Of files_items).Equals
    End Function

End Class
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.