Question
· Dec 10, 2018

How to get files from FTP Server

Hello!

I'm trying to get files from FTP server.  Bellow my code:

Set sc=$$$OK
    set ftp = ##class(%Net.FtpSession).%New()
    set ftp.Timeout = 5
    set host="speedtest.tele2.net"
    set port = 21
    
    If 'ftp.Connect(host,"USER","USER",port) Write ftp.ReturnMessage,! Quit sc
    Write "Ftp server messsage:",!,ftp.ReturnMessage,!
Set stream=##class(%GlobalCharacterStream).%New()
If 'ftp.Binary() Write "Can not swap to binary mode",! Quit sc
Write "Mode now: ",ftp.Type,!
If 'ftp.Retrieve("512KB.zip",stream) Write "Failed to get file",! Quit sc
Write "Length of file received: ",stream.Size,!
If 'ftp.Logout() Write "Failed to logout",!
Quit ftp.Logout()

I'm getting error:

Unable to open connection to speedtest.tele2.net on port 21. 

Discussion (5)0
Log in or sign up to continue

I'm getting following error, running your program on Caché 2017.2.2:

USER>do ^test
This FTP server is anonymous only.

And it works OK once I change Connect to be anonymous:

If 'ftp.Connect(host,"anonymous","",port) Write ftp.ReturnMessage,! Quit sc

Output:

USER>do ^test
Ftp server messsage:
Features:
 EPRT
 EPSV
 MDTM
 PASV
 REST STREAM
 SIZE
 TVFS
End
Mode now: Binary
Length of file received: 524288

Can you connect to speedtest.tele2.net from the same server, but not from Caché? Maybe access via port 21 is blocked by your firewall?