SAP ABAP - Put File to FTP Server
Maintain SAPFTP_SERVERS (SM30)
Test Program :
ABAP Code
*&---------------------------------------------------------------------*
*& Report YFTP_PUT_FILE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT yftp_put_file.
TYPES :
BEGIN OF ty_podata,
ebeln TYPE ebeln,
spr1(1) TYPE c, "cl_abap_char_utilities=>horizontal_tab,
ebelp TYPE ebelp,
spr2(1) TYPE c,
matnr TYPE matnr,
spr3(1) TYPE c,
txz01 TYPE ekpo-txz01,
END OF ty_podata.
TYPES: BEGIN OF text,
line(120) TYPE c,
END OF text.
DATA :
lt_podata TYPE TABLE OF ty_podata,
ls_podata TYPE ty_podata,
result TYPE TABLE OF text WITH HEADER LINE.
DATA: key TYPE i VALUE 26101957,
hdl TYPE i,
slen TYPE i,
x TYPE i,
docid LIKE sysuuid-c,
fname TYPE char30,
cmd(120),
error,
* bTEXT(120) type x,
bline(120) TYPE x.
PARAMETERS: user(30) TYPE c LOWER CASE,
pwd(30) TYPE c LOWER CASE,
host(64) TYPE c LOWER CASE,
pasv.
SELECTION-SCREEN SKIP 1.
PARAMETERS:
p_ebeln TYPE ebeln,
dest TYPE rfcdes-rfcdest DEFAULT 'SAPFTPA'.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-name = 'PWD'.
screen-invisible = '1'.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
START-OF-SELECTION.
PERFORM get_data.
PERFORM put_text_file.
FORM get_data.
REFRESH lt_podata.
SELECT ebeln ebelp matnr txz01 FROM ekpo INTO CORRESPONDING FIELDS OF TABLE lt_podata WHERE ebeln = p_ebeln.
LOOP AT lt_podata INTO ls_podata.
ls_podata-spr1 = ls_podata-spr2 = ls_podata-spr3 = cl_abap_char_utilities=>horizontal_tab.
MODIFY lt_podata FROM ls_podata
TRANSPORTING spr1 spr2 spr3
WHERE ebeln = ls_podata-ebeln AND ebelp = ls_podata-ebelp.
CLEAR ls_podata.
ENDLOOP.
ENDFORM.
FORM put_text_file.
IF lt_podata IS NOT INITIAL.
slen = strlen( pwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = pwd
sourcelen = slen
key = key
IMPORTING
destination = pwd.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = 'Connect to FTP Server'.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = user
password = pwd
host = host
rfc_destination = dest
IMPORTING
handle = hdl.
IF NOT pasv IS INITIAL.
REFRESH result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'set passive on'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
IF sy-subrc EQ 0.
WRITE: / 'Set passive mode'.
SKIP 1.
ENDIF.
ENDIF.
CLEAR fname.
CONCATENATE p_ebeln '.txt' INTO fname.
CALL FUNCTION 'FTP_R3_TO_CLIENT'
EXPORTING
fname = fname
rfc_destination = dest
* blob_length = slen
character_mode = 'X'
TABLES
* blob = bindata.
text = lt_podata.
CONCATENATE 'put' fname INTO cmd SEPARATED BY ' '.
REFRESH result.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = cmd.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
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
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = hdl.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = dest
EXCEPTIONS
OTHERS = 1.
ENDIF.
ENDFORM.
Comments
Post a Comment