Z.Aymen
Z.Aymen

Reputation: 5

how to dowload file from FTP server (LAN/ IP adress) in vb.net

hi every body i need help,idowloaded my excel file from the host;but the file is EMPTY(0k).I used this simple code:

'on event click
Dim address As String = "ftp://172.xx.xx.x/C:/aero_mes/A_0101.xls"
Dim locadress As String = "C:/bdcrq/A_0101.xls"
Try
    download = New WebClient
    download.DownloadFileAsync(New Uri(address), locadress)
Catch ex As Exception
    MsgBox(ex.Message)
End Try

Upvotes: 0

Views: 530

Answers (2)

dwilliss
dwilliss

Reputation: 924

I like Chillzy's answer, but the alternative is to just use DownloadFile() instead of DownloadFileAsync()

DownloadFileAsync will start the download in the background and you have to do something like what Chillzy suggested to get notified when it's done.

DownloadFile won't return until it's done so it's a lot easier.

The drawback to DownloadFile is that if you're on the foreground thread of a WinForms UI, no UI events can be processed while the file downloads. If it takes too long, you get the dreaded "not responding" white screen.

Another advantage to Chillzy's answer is that you can also add a DownloadProgressChanged event handler to get progress and update a ProgressBar.

Upvotes: 0

Chillzy
Chillzy

Reputation: 468

You are using an async method. You need to have an event handler to know when it is finished. Use This example and change it for your needs since it is based on your code.

 Sub DownloadFile()
            Dim address As String = "http://ftp.redhat.com/redhat/cdk/3.1.1/en/source/MD5SUM"
            Dim locadress As String = "C:\dump\MD5SUM"
            Dim download As WebClient
            Try
                download = New WebClient
                download.DownloadFileAsync(New Uri(address), locadress)
                AddHandler download.DownloadFileCompleted, AddressOf FinishDownload
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub


        Sub FinishDownload(sender As Object, e As AsyncCompletedEventArgs)
            Try

                '  If the request was not canceled and did not throw
                '  an exception, display the resource.
                If e.Cancelled = False AndAlso e.Error Is Nothing Then

                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

Upvotes: 1

Related Questions