There are few points need to be considered while sending file from sap to ftp server.
- 1. Check with basis team there are any connection exists from SAP to FTP server.
- 2. Need proper login credential.
- 3. There must a specific folder created to post files in FTP server OR the userid created for login must have write access to this particular folder where the files need to be posted.
To check this 3 thing you must run some standard report. Open SE38 and search SAPFTP* you will get list of reports which are for different purpose.
Mainly RSFTP002 fulfills these purposes.
To check all these 3 things are necessary FTP log on username
password
destination IP address (HOST)
folder path (remote path)
RFC destination (SAPFTP or SAPFTPA)
Step 1. execute the report
Provide all the credentials as given above and execute the report. If there is an FTP connection exists then the same screen will appear after executeion .
If there is anything wrong then below message will appear.
Step 2. execute the same report again to know whether it has access to that folder or not.
This time in command field give the path as in above screen shot. 'cd '(pass in small leter) is the command to get directory i.e cd for change directory. Specify the exact path as it is case sensitive. Give a space between 'cd' &'path'.
If it has access to this folder it will navigate to next screen "command successfull" & else in next screen you will get command error or unsuccessful. Then FTP team need to be contacted to get access for the particular folder for this given user ID.
After getting everything correct now you can start your coding to send files from SAP to FTP.
In the previous doc all the pre necessary things are covered to move files from SAP to FTP .
There are certain function module provided by sap to send files ( create files in FTP server).
There are two ways to send a files from sap to FTP .
FM to be called in sequence
Approach 1 | Approach 2 | Purpose of each FM |
HTTP_SCRAMBLE | HTTP_SCRAMBLE | Get encrypted pwd from pwd |
FTP_CONNECT | FTP_CONNECT | To establish a connection |
FTP_COMMAND | FTP_COMMAND | To open the specific folder command name is 'cd ' |
FTP_R3_TO_SERVER | FTP_COMMAND | To post the file/create the file in approach two command name is 'put' |
FTP_DISCONNECT | FTP_DISCONNECT | To disconnect the FTP connection |
RFC_CONNECTION_CLOSE | RFC_CONNECTION_CLOSE | To close the RFC destination |
How to code.
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = v_pwd " the password used in the report for example v_pwd = 'QFRTY' alll in caps.
sourcelen = v_len " length of the password v_len = strlen(v_pwd)
key = v_key " for FTP connection it is always v_key = '26101957'
IMPORTING
DESTINATION = v_pwd_crypt " encrypted password to use it will be like 'DSK75JDOWK3'
.
The above FM is used to convert the password so that SAP can understand the password and it will be used in next function module for use.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = v_user " user id used in the report for example v_user = 'FTPUSR001'
password = v_pwd_crypt " ENCRYPTED PASSWORD WE GOT FROM PREVIOUS FM
host = v_IP " IP address of the target server
rfc_destination = v_dest " this is connection type it's value can be 'SAPFTP' or 'SAPFTPA'(for background processing)
IMPORTING
HANDLE = lv_handle . " a handle will be generated & will be used in next FM's
The above FM is called to open the connection between SAP & FTP server.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
HANDLE = lv_handle " this we get from previous FM
command = 'cd /directory path/' "specify the path to put file from sap eg '/home/test/output' .before passing this, concatenate 'cd 'to the
tables "path and pass it
data = lt_data . " this table will give all the messages returned from the FM.
This above FM is usefull to specify the location where you want to put the file.
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = lv_handle
fname = f_name "specify the file name & it is just file name like 'test.txt'
CHARACTER_MODE = 'X' " transfer the data as character
TABLES
TEXT = gt_data. " pass the internal table that holds the data
The above FM is used to generate file at the location which is given by previous FM 'FTP_COMMAND'.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = lv_handle
.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
DESTINATION = v_dest . "this will be SAPFTP or SAPFTPA.