Article
· Jun 20 2m read

How to upload/download image files from an FTP server

InterSystems FAQ rubric

The procedure for uploading/downloading from an FTP server is as follows.

1. Upload the image file to the FTP server

 set tmpfile="c:\temp\test.jpg"
 set ftp=##class(%Net.FtpSession).%New() 
 // connect to FTP server
 do ftp.Connect("","<username>","<password>")
 // set transfer mode to BINARY
 do ftp.Binary()
 // Move to the directory to upload
 do ftp.SetDirectory("/temp/upload")
 // Prepare a stream of files to upload  
 set file=##class(%File).%New(tmpfile)
 do file.Open("UK\BIN\")
 // upload file
 // 1st argument: File name to create at upload destination
 // 2nd argument: File stream to upload
 do ftp.Store("test.jpg",file)
 // Logout from ftp server
 do ftp.Logout()
 // close the file
 do file.Close()
 // (Optional) Delete the uploaded file
 //do ##class(%File).Delete(tmpfile)

2. Download the image file from the FTP server

   set ftp=##class(%Net.FtpSession).%New()     // Connect to ftp server
    do ftp.Connect("","<username>","<password>")     // Set the transfer mode to BINARY
    do ftp.Binary()     // Prepare a file stream to download and store
    set stream=##class(%FileBinaryStream).%New()
    do stream.LinkToFile("c:\temp\testdownload.jpg")
   // Go to the download directory
    do ftp.SetDirectory("/temp/download")     // Download the file and close the stream
    do ftp.Retrieve("test.jpg",stream)
    do stream.SaveStream()
    Set stream=""     // Logout from the ftp server
    do ftp.Logout()

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