Access FTP Server from SAP!

Hi All!

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.

 Selection 
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(30TYPE c LOWER CASE,                       "Username
            p_pwd(30TYPE c LOWER CASE.                        "Password
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS: p_ip(64TYPE 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





Labels: , , , , , , ,