Question FtpResponse Content Length -1

Dan181920

Active member
Joined
Jun 28, 2011
Messages
27
Programming Experience
Beginner
Hi everyone,

I am writing an application that downloads files from an FTP site in VB.Net, depending on the file name provided in the textbox on the screen.

The program currently writes the file to my desktop, however, the content of the file is not being downloaded, therefore the file size is 0 bytes.

Below is my code, can anybody advise on how I can edit my code so that my application not only creates the file on my desktop, but then when I open the file, the content is also there.

I feel at the moment that my problem lies with the way I have supplied the textbox details in the WebRequest decalartion, but I am not yet sure.

Any help would be appreciated.

Thanks in advance,

Dan


Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click


Dim
ftp As FtpWebRequest = DirectCast(WebRequest.Create(ftp://XX.XX.X.XX/In/ + TextBox1.Text), FtpWebRequest)
If
TextBox1.Text = "" Then

MsgBox("A file needs to be requested!") End If



MsgBox("BEGINNING...")

If
TextBox1.Text <> "" Then
Try

ftp.Credentials = New System.Net.NetworkCredential(User, password) ftp.KeepAlive = False
ftp.Timeout = 20000

ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile ftp.UseBinary = True


' read in file...


Dim filestream As New FileStream("C:\Users\" + TextBox1.Text, FileMode.Append) Dim buffer(filestream.Length) As Byte

MsgBox("CONTINUING...")
filestream.Write(buffer, 0, filestream.Length)

MsgBox("CONTINUING MORE...")

' download file...

Dim GetStream As Stream = ftp.GetRequestStream() GetStream.Write(buffer, 0, filestream.Length)
GetStream.Close()
filestream.Close()


Dim
ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
MsgBox("FINISHED...")
Catch
ex As Exception
End Try
End If
End Sub
 
I believe this is your problem?
' download file...

Dim GetStream As Stream = ftp.GetRequestStream() 
GetStream.Write(buffer, 0, filestream.Length)
GetStream.Close()
filestream.Close()


See this FTP demo from codeproject, it's in C# but all the logic is there: Simple FTP demo application using C#.Net 2.0 - CodeProject
Here's a translator you can use if your unfamiliar with c# http://www.developerfusion.com/tools/convert/csharp-to-vb/

Here's a part of the download portion translated and partially rewritten
            Dim ftpReponse As FtpWebResponse = ftp.GetResponse
            Dim ftpStream As Stream = ftpReponse.GetResponseStream

            Dim fileName As String = "someFile.html" 'or whatever file ext
            Dim fileOutputStream As New FileStream("C:\" & fileName, FileMode.Create)

            Dim cl As Long = ftpReponse.ContentLength
            Dim bufferSize As Integer = 2048
            Dim readCount As Integer
            Dim buffer(bufferSize) As Byte

            readCount = ftpStream.Read(buffer, 0, bufferSize)
            While (readCount > 0)
                fileOutputStream.Write(buffer, 0, readCount)
                readCount = ftpStream.Read(buffer, 0, bufferSize)
            End While
 
Last edited:
Back
Top