• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

FTP download file code Review

I am using an example from a Networking Programming book called: "Network Programming in .NET" by Fiach Reid.

The code works perfectly uploading file to a FTP site but it does not work when downloading. I dont get any error messages, the program simply hangs and stops responding after the download button is clicked.

Help is appreciated on this issue.

I am attaching a code snipet for reference.

Thank you
Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
 
        Dim filedata As Byte()
 
        SaveFileDialog1.ShowDialog()
        filedata = sendPassiveFTPcmd("RETR " + lstFiles.SelectedItem.ToString() + vbCrLf)
 
        Dim fs As FileStream = New FileStream(SaveFileDialog1.FileName, FileMode.CreateNew)
 
        fs.Write(filedata, 0, filedata.Length)
 
        fs.Close()
 
   End Sub
 
 
 
Public Function sendPassiveFTPcmd(ByVal cmd As String) As Byte()
 
        Dim szData() As Byte
        Dim al As New System.Collections.ArrayList
        Dim bytes As Int32
        Dim RecvBytes(Byte.MaxValue) As Byte
 
        szData = System.Text.Encoding.ASCII.GetBytes(cmd.ToCharArray())
 
        Dim totalLenght As Int32 = 0
 
        Dim PassiveConnection As NetworkStream
        PassiveConnection = createPassiveConnection()
        txtStatus.Text += vbCrLf + "Sent: " + cmd
 
        Dim commandStream As StreamReader = New StreamReader(NetStrm)
 
        NetStrm.Write(szData, 0, szData.Length)
 
        Do While (True)
 
            bytes = PassiveConnection.Read(RecvBytes, 0, RecvBytes.Length)
 
            If bytes <= 0 Then Exit Do
            totalLenght += bytes
            al.AddRange(RecvBytes)
        Loop
 
        al = al.GetRange(0, totalLenght)
 
        txtStatus.Text += vbCrLf + "Rcvd:" + commandStream.ReadLine() '125
        txtStatus.Text += vbCrLf + "Rcvd:" + commandStream.ReadLine()
 
        Return CType(al.ToArray((New Byte).GetType()), Byte())
 
 
    End Function

Open in new window

0
virgilar
Asked:
virgilar
  • 3
  • 3
1 Solution
 
drichardsCommented:
Where does the code hang?
0
 
virgilarAuthor Commented:

The filedata variable never gets populated. Although, the function SendPassiveFTPcmd seems to work fine (since it does not error out), it never returns a value to populate the filedata variable.

filedata = sendPassiveFTPcmd("RETR " + lstFiles.SelectedItem.ToString() + vbCrLf)

I believe that the problem is on the return statement. Thats supposed to return the bytes back to the variable on the download_click routine.

Return CType(al.ToArray((New Byte).GetType()), Byte())

0
 
virgilarAuthor Commented:
I found out what the problem is:

If you take a look at the picture attached the file name includes the date, time and size on the string. I hard coded a file name in the code and ran the download and everything worked fine.

So my problem is that i have to display the file names only on my list box instead. I have to trim the file name some how. The code below show how i add the file name to the list box.

The previous code i had included on my message works fine. The problem lies on how the string containing the file name is read by the download routine.


Public Sub GetRemoteFolders()
 
        Dim FilesandFolders() As String
        Dim FileorFolder As String
        Dim FolderList As String = ""
        Dim lastSpace As Integer = 0
 
        FolderList = Encoding.ASCII.GetString(sendPassiveFTPcmd("List" + vbCrLf))
 
        lstFiles.Items.Clear()
 
        lstFolder.Items.Clear()
 
        FilesandFolders = FolderList.Split(vbCr.ToCharArray())
 
        Dim i As Integer
 
        For i = 0 To FilesandFolders.GetUpperBound(0) - 1
 
            If FilesandFolders(i).StartsWith("-") Or FilesandFolders(i).StartsWith("d") Then
                lastSpace = 59 ' Unix Format
            Else
                lastSpace = 39 ' Dos Format
            End If
 
            FileorFolder = FilesandFolders(i).Substring(lastSpace)
 
            If FileorFolder.IndexOf(".") <> -1 Then
                lstFiles.Items.Add(FileorFolder.Trim())
            Else
                lstFolder.Items.Add(FileorFolder.Trim())
            End If
        Next
    End Sub

Open in new window

0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
drichardsCommented:
As I look at it again, the problem is more likely in the ReadLine of the commandStream.  I don't know the FTP spec i nthat detail.  Is it stuck waiting for the command response?

That woudl seem to be the most likely thing if it's hung, as you initially describe.  You can put a breakpoint on the return statement and see the state of things at that point.  If it never gets there, you can break and see which line it's on.
0
 
drichardsCommented:
Sorry, I'm watching football here and didn't see that you had already posted the solution.
0
 
virgilarAuthor Commented:
No problem. I am doing the same here. LOL.  Thanks for your help on this.

I will do some research and try to find out how to display the file name only. That should be it.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now