FTP stands for File transfer protocol. It is basically used to transfer files from the local computer to the client/remote server and manage client and server side data of the application. 

Ruby provides a ‘net/ftp’ library to implement FTP into your web application.

Experts at any Ruby on Rails development company need to understand how to integrate FTP in a rails application. Here you go.

In this article we will learn how to 

  • Integrate FTP in rails application.
  • Methods of FTP library and use of these methods.

First you need a library of FTP class.

require 'net/ftp'

Get Estimate for Ruby on Rails Application

There are some methods which will be useful to users.

  • ::open
  • getbinaryfilefile
  • gettextfile
  • putbinaryfile
  • puttextfile
  • chdir
  • rename
  • Delete
  • Login

1. ::open

This method is similar to FTP.new with mandatory host parameters.

Users have to provide the mandatory parameters as an argument in this method.

This method will pass to the FTP object and make a connection to the remote server and close it when the blocks finish or exception is raised.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:
SFTP_CONFIGURATION_USERNAME, password: SFTP_CONFIGURATION_PASSWORD) do
|ftp|

end

2. Getbinaryfilefile(remote_file_path, local_file_path, blocksize = DEFAULT_BLOCKSIZE )

This method is used for retrieving files from the remote server in binary mode and storing the results in a specified  local file. If the local file is nil, it returns retrieved data.

DEFAULT_BLOCKSIZE value is by default 1024 bytes per block(Not Mandatory).

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.getbinaryfile("/uploads/tmp_data/inventory_30_09_2020.xls",
'/home/er/projects/inventory.xls', blocksize = DEFAULT_BLOCKSIZE)
end

3. Gettextfile(remote_file_path, local_file_path)

This method retrieves files in ASCII(text) mode from the remote server and stores the results in a specified  local file. If the local file is nil, it returns retrieved data. 

If blocks supplied it is passed retrieved data one line at a time.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.gettextfile("/uploads/tmp_data/inventory_30_09_2020.xls",
'/home/er/projects/inventory.xls', blocksize = DEFAULT_BLOCKSIZE)
end

4. Putbinaryfile(local_file_path, remote_file_path)

Transfer local file to the remote server in binary mode at the specified file location given in the remotefile parameter and store the data into the remote file.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.putbinaryfile('/home/er/projects/inventory.xls',
"/uploads/tmp_data/inventory_30_09_2020.xls")
end

5. Puttextfile

Transfer local file to the remote server in ASCII(text) mode at the specified file location given in the remotefile parameter and store the data into the remote file.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.puttextfile('/home/er/projects/inventory.xls',
"/uploads/tmp_data/inventory_30_09_2020.xls")
end

6. Chdir

Changes the remote directory.

List method returns all the files of the current remote directory.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:SFTP_CONFIGURATION_USERNAME,
password: SFTP_CONFIGURATION_PASSWORD) do |ftp|
ftp.chdir("uploads/")
Files = ftp.list
end

7. Rename

Used for renaming a file.

Net::FTP.open(SFTP_CONFIGURATION_URL, username:
SFTP_CONFIGURATION_USERNAME, password: SFTP_CONFIGURATION_PASSWORD) do
|ftp|
rename('stock_data.txt', 'all_stock_details.txt')
end

8. Delete

Used to delete files from the remote server.

def delete
Filename = '/uploads/tmp_data/inventory_30_09_2020.xls'
resp = sendcmd("DELE #{filename}")
end

So, using the above method and the use of getbinaryfile and putbinaryfile, we can download files from the FTP server and send the file to the FTP server.