Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

File Transfer Using FTP Commands

Former Member
0 Likes
5,968

Hi,

I have a file sitting in my SAP dir which i want to sent this file to a web server using 'FTP' any sample code would be helpfull

Thanks,

Kumar

8 REPLIES 8
Read only

Former Member
0 Likes
2,417

Hi,

Refer to this link..http://docs.google.com/Doc?id=dfv2hmgs_21g5nmwkgw&hl=en

Read only

0 Likes
2,417

Hi ,

Can u pls eloberate on the FM FTP_CONNECT , rfc_destination indicating to and the difference b/w FM FTP_R3_TO_SERVER and FM FTP_R3_TO_CLIENT. In my case which of the FM module should i use .

Thanks,

Kumar

Read only

Former Member
0 Likes
2,417

Hi,

FM you need to use to transfer data from sap to FTP server

1. HTTP_SCRAMBLE

2. FTP_CONNECT

3. FTP_COMMAND

4. FTP_R3_TO_SERVER

5. FTP_DISCONNECT

For more help you can check the demo progam RSFTP007 or RSFTP*

Refer to this link..http://abap-gallery.blogspot.com/2007/07/ftp.html

Read only

Former Member
0 Likes
2,417

Hi Kumar,

Check the Below code to Transfer File using FTP

Specify FTP Username, password and Host and Specify Destination Directory(Where you want to Place the file )

*Program

DATA: w_password(30) TYPE c,

w_length TYPE i,

w_key TYPE i VALUE 26101957,

w_handle TYPE i,

w_command(500) TYPE c,

slen type i.

data : source_path(250),

dest_path(250).

types: begin of text,

line(800) type c,

end of text.

data: IT_INPUT type table of text with header line.

data: ftp_session type table of text with header line.

DATA : V_FILE_NAME LIKE rlgrap-filename.

data: v_docid(20) type c.

SELECTION-SCREEN : BEGIN OF BLOCK ONE WITH FRAME TITLE TEXT-001.

parameters: P_USER(30) type c lower case obligatory,

P_PWD(30) type c lower case obligatory,

P_HOST(64) type c lower case obligatory.

SELECTION-SCREEN : END OF BLOCK ONE .

SELECTION-SCREEN : BEGIN OF BLOCK TWO WITH FRAME TITLE TEXT-002.

SELECT-OPTIONS P_INFILE LIKE V_FILE_NAME.

SELECTION-SCREEN : END OF BLOCK TWO .

selection-screen skip 1.

parameters: dest type rfcdes-rfcdest default 'SAPFTPA' obligatory.

*Scramble Password

w_length = STRLEN( P_PWD ).

CALL FUNCTION 'HTTP_SCRAMBLE'

EXPORTING

SOURCE = 'mainframe'

sourcelen = w_length

key = w_key

IMPORTING

destination = w_password.

  • Connect to FTP destination (DEST_HOST)

CALL FUNCTION 'FTP_CONNECT'

EXPORTING

USER = P_USER

password = w_password

HOST = P_HOST

rfc_destination = DEST

IMPORTING

handle = w_handle

EXCEPTIONS

not_connected = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4

RAISING cannot_connect.

ENDIF.

REFRESH ftp_session.

  • Navigate to destination directory

dest_path = 'Your Dest Directory on web server'.

CONCATENATE 'cd' dest_path INTO w_command SEPARATED BY space.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

handle = w_handle

command = w_command

TABLES

data = ftp_session

EXCEPTIONS

command_error = 1

tcpip_error = 2.

IF sy-subrc <> 0.

CONCATENATE 'FTP command failed:' w_command

INTO w_command SEPARATED BY space.

MESSAGE ID 'ZW' TYPE 'E' NUMBER '042'

WITH w_command

*Read Input file

data : vl_filepath(200).

refresh IT_INPUT.

*Read File from Appserver

OPEN DATASET FP_INFIL FOR INPUT IN TEXT MODE TYPE 'NT'.

IF sy-subrc NE 0.

WRITE : / 'ERROR OPENING FILE :' , FP_INFIL COLOR COL_NEGATIVE.

STOP.

ENDIF .

**********READ THE DATA TO TABLE FP_DATA_TAB

DO.

READ DATASET FP_INFIL INTO IT_INPUT.

IF sy-subrc <> 0.

EXIT.

ENDIF.

*Transfer Data

slen = 800.

docid = FP_FILENAME.

call function 'FTP_R3_TO_CLIENT'

exporting

fname = docid

rfc_destination = dest

blob_length = slen

character_mode = 'X'

tables

text = IT_INPUT.

  • blob = bindata.

if sy-subrc ne 0.

refresh IT_INPUT.

return.

endif.

*Command

concatenate 'put' docid into cmd separated by ' '.

refresh result.

call function 'FTP_COMMAND'

exporting

handle = w_handle

command = cmd

tables

data = result

exceptions

tcpip_error = 1

command_error = 2

data_error = 3.

if sy-subrc ne 0.

  • error = 1.

exit.

endif.

loop at result.

write: / result-line.

endloop.

skip 1.

*Disconnect Host

CALL FUNCTION 'FTP_DISCONNECT'

EXPORTING

handle = w_handle.

APPEND IT_INPUT.

CLEAR IT_INPUT.

ENDDO.

**********CLOSE THE FILE FP_INFIL**************

CLOSE DATASET FP_INFIL.

RAISING dest_path_unknown.

ENDIF.

Thanks,

Kishore

Read only

Former Member
0 Likes
2,417

Check part of sample code below for reference


data:
  FTP_USER(64) value 'sielroot', "Case sensitive
  FTP_PWD(64) value 'siel//?!',  "Case Sensitive
  FTP_HOST(50) value '107.10.86.162', "I.P add of FTP server
  RFC_DEST LIKE RSCAT-RFCDEST value 'SAPFTPA'.  "Case sensitive
DATA:
  HDL TYPE I,
  KEY TYPE I VALUE 26101957,
  DSTLEN TYPE I,
  cmd(255).
DATA:
  BEGIN OF FTP_DATA OCCURS 0,
    LINE(132) TYPE C,
  END OF FTP_DATA.

DESCRIBE FIELD FTP_PWD LENGTH DSTLEN.

FORM FTP.
***Encrypt Password.
CALL 'AB_RFC_X_SCRAMBLE_STRING'
      ID 'SOURCE' FIELD FTP_PWD
      ID 'KEY' FIELD KEY
      ID 'SCR' FIELD 'X'
      ID 'DESTINATION' FIELD FTP_PWD
      ID 'DSTLEN' FIELD DSTLEN.

***Connect to FTP
CALL FUNCTION 'FTP_CONNECT'
     EXPORTING
          USER            = FTP_USER
          PASSWORD        = FTP_PWD
          HOST            = FTP_HOST
          RFC_DESTINATION = RFC_DEST
     IMPORTING
          HANDLE          = HDL
     EXCEPTIONS
          NOT_CONNECTED   = 1
          OTHERS          = 2.
IF SY-SUBRC NE 0.
  WRITE:/ 'COULD NOT CONNECT TO', FTP_HOST.
ELSE.

***Create File on Application Server
  PERFORM CREATE_FILE_ON_APP_SER.

  CALL FUNCTION 'FTP_DISCONNECT'
       EXPORTING
            HANDLE = HDL
       EXCEPTIONS
            OTHERS = 1.
  IF SY-SUBRC NE 0.
    WRITE:/ 'COULD NOT DISCONNECT FROM FTP SERVER'.
  ELSE.
    WRITE:/ 'DISCONNECTED FROM FTP SERVER'.
  ENDIF.

ENDIF.
ENDFORM.                    " FTP

Read only

0 Likes
2,417

continue... PART II


FORM CREATE_FILE_ON_APP_SER.
data:
  file type string,
  ftp_file type string.

***Choose/Create a path on Application Server through TCode AL11
***and assign it to FILE variable, as an e.g. i have taken '/tmp'
file = '/tmp'.  "Path
concatenate file '/itab.txt' into file. "Path with filename
ftp_file = '/Product_DEV/itab.txt'.

open dataset file for output in text mode.
if SY-SUBRC = 0.
  write : /  'File opened on application server' .
else.
  write : /  'Unable to open file on application server'.
endif.

  loop at itab.
    transfer itab to file.
  endloop.
  if SY-SUBRC = 0.
    write : /  'Data transfer to file on application server complete.' .
  else.
    write : /  'Unable to transfer data to file on application server.'.
  endif.

  close dataset file.
  if SY-SUBRC = 0.
    write : /  'File closed on application server' .
  else.
    write : /  'Unable to close file on application server'.
  endif.

***Copy file from application server to FTP
concatenate 'put' file ftp_file into cmd SEPARATED by space.
CALL FUNCTION 'FTP_COMMAND'
     EXPORTING
          HANDLE        = hdl
          COMMAND       = cmd
     TABLES
          DATA          = FTP_DATA
    EXCEPTIONS
         TCPIP_ERROR   = 1
         COMMAND_ERROR = 2
         DATA_ERROR    = 3
         OTHERS        = 4          .
IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

***Delete file from App. Server
  delete dataset file.
  if SY-SUBRC = 0.
    write : /  'File deleted from application server' .
  else.
    write : /  'Unable to delete file from application server'.
  endif.

ENDFORM.                    " CREATE_FILE_ON_APP_SER

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,417

check with this Fm too SXPG_COMMAND_EXECUTE

Read only

Former Member
0 Likes
2,417

issue closed