Click here to Skip to main content
15,939,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I got problem to download file from ftp
this is my simple code

Private Function FTPRequestFile() As Boolean
        Dim ftpURI As String
        Dim nRead As Integer
        Dim speedtimer As New Stopwatch
        Dim currentspeed As Double = -1
        Dim readings As Integer = 0
        Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
        l_ExtractProgress.Text = "Preparing to download ..."
            ftpURI = "ftp://" & ConfigX.FTPServer & "/" & ConfigX.FTPFilePath & "/" & ConfigX.SaveFileName
            Dim FTPRequest As FtpWebRequest = FtpWebRequest.Create(ftpURI)
            With FTPRequest
                .EnableSsl = False
                .Credentials = New NetworkCredential(ConfigX.FTPUser, ConfigX.FTPPassword)
                .KeepAlive = True
                .UseBinary = True
                .UsePassive = True
                .Timeout = Timeout.Infinite
                .Method = System.Net.WebRequestMethods.Ftp.DownloadFile
            End With
            Using FTPResponse As System.Net.FtpWebResponse = CType(FTPRequest.GetResponse, System.Net.FtpWebResponse)
                Using responseStream As IO.Stream = FTPResponse.GetResponseStream
                    Dim length As Long = FTPResponse.ContentLength 'Size of the response (in bytes)
                    Using fs As New IO.FileStream(ConfigX.SaveFilePath + ConfigX.SaveFileName, IO.FileMode.Create)
                        Dim buffer(2047) As Byte
                        Dim read As Integer = 0
                            If BackgroundWorker1.CancellationPending Then 'If user abort download
                                Exit Do
                            End If
                            Dim readBytes(4095) As Byte
                            read = responseStream.Read(buffer, 0, buffer.Length)
                            nRead += read
                            Dim percent As Short = (nRead * 100) / length
                            Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
                            If read = 0 Then Exit Do
                            fs.Write(buffer, 0, read)
                            readings += 1
                            If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
                                currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                                readings = 0
                            End If
                        Loop Until read = 0
                    End Using
                End Using
            End Using
            Return True
        Catch ex As Exception
            Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
            Me.Invoke(cancelDelegate, True)
            Return False
        End Try
    End Function

in my laptop this code is running well, but in my another laptop i got problem at line "Dim length As Long = FTPResponse.ContentLength"
i get length -1 finaly file can not be download.
please help my.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900