Access FTP Server from SAP!

This will show you how to transfer data from SAP to FTP server location. Program is a example of how you can connect to FTP and create a text file.

There are several methods to write a file to FTP, I am using the method of transferring data in internal table directly to FTP using FM ” FTP_R3_TO_SERVER”. Other method is to use FM ” FTP_COMMAND ” with DOS commands.

 FTP access program selection screen:

DATA: lc_key TYPE i VALUE 26101957,
lc_rfc_dest LIKE rfcdes-rfcdest VALUE ‘SAPFTPA’.          “RFC Destination

DATA: lv_pwd_len TYPE i,
lv_handle TYPE i.

DATA: li_data TYPE TABLE OF t001,
li_result TYPE TABLE OF text WITH HEADER LINE.

selection-screen BEGIN OF block b1 WITH frame title text-001.
PARAMETERS: p_user(30) TYPE c LOWER CASE,                       “Username
p_pwd(30) TYPE c LOWER CASE.                        “Password
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS: p_ip(64) TYPE c.                                    “FTP IP
SELECTION-SCREEN END OF BLOCK b2.

AT SELECTION-SCREEN OUTPUT.

*password should be hidden
LOOP AT SCREEN.
IF screen-name = ‘P_PWD’.
screen-invisible = ‘1’.
MODIFY SCREEN.
ENDIF.
ENDLOOP.

START-OF-SELECTION.
* get data to be transferred
SELECT *
FROM t001
INTO TABLE li_data.

END-OF-SELECTION.

*  get the length of the password
lv_pwd_len = STRLEN( p_pwd ).

*  get the hexa decimal password
CALL FUNCTION ‘HTTP_SCRAMBLE’
EXPORTING
SOURCE      = p_pwd
sourcelen   = lv_pwd_len
key         = lc_key
IMPORTING
destination = p_pwd.

*  connect to FTP server
CALL FUNCTION ‘FTP_CONNECT’
EXPORTING
user            = p_user
password        = p_pwd
host            = p_ip
rfc_destination = lc_rfc_dest
IMPORTING
handle          = lv_handle.

*passive On
CALL FUNCTION ‘FTP_COMMAND’
EXPORTING
handle        = lv_handle
command       = ‘set passive on’
TABLES
data          = li_result
EXCEPTIONS
tcpip_error   = 1
command_error = 2
data_error    = 3.

REFRESH li_result.

*set ASCII mode
CALL FUNCTION ‘FTP_COMMAND’
EXPORTING
handle        = lv_handle
command       = ‘ascii’
TABLES
data          = li_result
EXCEPTIONS
tcpip_error   = 1
command_error = 2
data_error    = 3.

*Transfer data in internal table li_data to FTP
CALL FUNCTION ‘FTP_R3_TO_SERVER’
EXPORTING
handle         = lv_handle
fname          = ‘Company_code.txt’
character_mode = ‘X’
TABLES
text           = li_data
EXCEPTIONS
tcpip_error    = 1
command_error  = 2
data_error     = 3
OTHERS         = 4.

* Disconnect the FTP connection
CALL FUNCTION ‘FTP_DISCONNECT’
EXPORTING
handle = lv_handle.

*Disconnect the RFC connection between SAP and FTP.
CALL FUNCTION ‘RFC_CONNECTION_CLOSE’
EXPORTING
destination = lc_rfc_dest
EXCEPTIONS
OTHERS      = 1.

Encode Message Payload to Base64 on CPI!

How to use Base64 message encoder in SAP Integration Suite.

Subscribe for more

My First Interface on CPI!

Learn how to develop your first iFlow on SAP Integration Suite within 7 minutes!

Subscribe for more

5 thoughts on “Access FTP Server from SAP!

Leave a Reply

Your email address will not be published. Required fields are marked *