0 votes
by (120 points)
edited

Hello, I want to allow the user to download a large file via FTP. With the code below I can do this but I want to be able to have the user select where to put the file. How can I modify the code to open a saveas dialog then use that path to save the file?

Dim client As New Ftp
client.Connect("ftp.example.org")
client.Login("username", "password")

' download the 'test.zip' file from the current directory at the server'
client.GetFile("test.zip", "c:\data\test.zip")

' disconnect '
client.Disconnect()
Applies to: Rebex FTP/SSL

2 Answers

0 votes
by (18.0k points)
edited

The common OpenFileDialog, SaveFileDialog or FolderBrowserDialog (from System.Windows.Forms namespace) cannot be used for browsing files on remote FTP server.

You have to design your own dialog with a System.Windows.Forms.ListView control or with some custom list control. Then fill it with a list of files and directories, which you can obtain from a FTP server using the Ftp.GetList method.

Here is a very simple sample:

    ' get a list of items from current FTP directory'
    Dim list = client.GetList()

    ' fill the items into a listView GUI control '
    For Each item In list
        Dim name As String
        name = item.Name
        If item.IsDirectory Then
            name = name & "\" 
        End If
        listView.Items.Add(name)
    Next

For a more advanced sample, see the WinFormClient sample project (part of samples installed with the Rebex FTP/SSL component).

by (150 points)
edited

I could be reading it wrong, but I don't think he wants the dialog to choose which file on the FTP server he wants. I think he only wants to choose the destination. Using one of the above mentioned dialog classes you can get a desired file path from the user, and then you could add that to the second parameter of GetFile(). This should not depend on the FTP library in any way.

by (70.2k points)
edited

You are right. Jan misunderstood the sentence "where to put the file" (not to put on the server, but to put on the local).

How to use System.Windows.Forms.SaveFileDialog is shown in the WinFormGet sample.

by (120 points)
edited

Thanks! to clarify I'm wanting to allow the user to select where they want to ftp/download the file onto their machine.

0 votes
by (18.0k points)
edited

I'm sorry for the misunderstanding.

Here is a right example, how to choose a location for a downloaded file using FileSaveDialog:

    '' connect to the FTP server
    Dim client As New Ftp
    client.Connect("servername")
    client.Login("username", "password")
    client.ChangeDirectory("remote_directory")

    '' an example of file to be downloaded
    Dim filename As String = "sample.txt"

    '' configure the SaveFileDialog
    Dim saveFileDialog As New SaveFileDialog
    saveFileDialog.FileName = filename  '' suggest the remote filename as a default target filename
    saveFileDialog.InitialDirectory = "c:\temp"

    '' open the the dialog and let user to choose the target file
    If saveFileDialog.ShowDialog() = DialogResult.OK Then
        '' let the dialog to open the target file
        Using stream = saveFileDialog.OpenFile()
            '' download data from FTP server to the opened target file
            client.GetFile(filename, stream)
        End Using
    End If

    client.Disconnect()
by (120 points)
edited

Thank you Jan! I will run some tests and let you guys know how it turns out.

...