Skip to Content
1
Jun 14, 2016 at 12:32 PM

Command Error from FTP_R3_TO_SERVER

3088 Views

Hi Experts,

I am facing one issue of Read & Write the csv file from FTP Server.

If i try to Write the csv file from SAP to FTP server, File name is creating with given name and given extension but When i open this file value is are not getting stored.

and Also FM FTP_R3_TO_SERVER giving Command error.

Kindly anyone help me out to resolve the issue.

Below my code.

SELECTION-SCREEN: BEGIN OF BLOCK a WITH FRAME TITLE text-001.
PARAMETERS:
pa_user TYPE c LENGTH 30 LOWER CASE OBLIGATORY,"FTP Server User
pa_pswrd TYPE c LENGTH 30 LOWER CASE OBLIGATORY ,"FTP Server User's Password
pa_host TYPE c LENGTH 64 LOWER CASE OBLIGATORY,"IP Address of the FTP Server
pa_rfcds LIKE rfcdes-rfcdest OBLIGATORY DEFAULT 'SAPFTP'. "RFC Destination,SAPFTP for Frontend communications(Local Connections) .
SELECTION-SCREEN: END OF BLOCK a.

DATA:
mi_key TYPE i VALUE 26101957,"Hardcoded Handler Key,This is always '26101957'
mi_pwd_len TYPE i ,"For finding the length of the Password,This is used when scrambling the password
mi_handle TYPE i."Handle for Pointing to an already connected FTP connection,used for subsequent actions on the connected FTP session

DATA:
it_sflight TYPE STANDARD TABLE OF crmd_orderadm_h,"Final Internal table to be uploaded as a Text file on an FTP Server
wa_sflight LIKE LINE OF it_sflight.

Data : begin of it_final occurs 0 ,
str(125) ,
end of it_final,
wa_final like line of it_final.

"FTP Communication Data Objects - [END]

SET EXTENDED CHECK OFF.
mi_pwd_len = STRLEN( pa_pswrd ).

CALL FUNCTION 'HTTP_SCRAMBLE'"For Encrypting the Password
EXPORTING
SOURCE = pa_pswrd
sourcelen = mi_pwd_len
key = mi_key
IMPORTING
destination = pa_pswrd.

CALL FUNCTION 'FTP_CONNECT'"For connecting to the FTP Server's user directory
EXPORTING
user = pa_user
password = pa_pswrd
host = pa_host
rfc_destination = pa_rfcds "Using the Background SAP FTP Library as part of the SAP backend System
IMPORTING
handle = mi_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.

IF sy-subrc = 0."When FTP connection established Successfully

SELECT * from crmd_orderadm_h
INTO TABLE it_sflight UP TO 10 ROWS.
*
* APPEND wa_sflight to it_sflight.
*
* "Final Internal table with a String
* concatenate wa_sflight-carrid wa_sflight-connid wa_sflight-fldate wa_sflight-planetype "wa_sflight-seatsmax wa_sflight-seatsocc
* into wa_final-str SEPARATED BY ' '.
* APPEND wa_final to it_final.

* ENDSELECT.

LOOP AT it_sflight INTO wa_sflight.
CONCATENATE wa_sflight-object_id wa_sflight-process_type INTO wa_final SEPARATED BY ','.
APPEND wa_final to it_final.
ENDLOOP.


DATA : path(58) ."Path that points to the FTP User's Home Directory
DATA:extension(4).
extension = '.CSV' .

CONCATENATE '/exportcsv' sy-datum sy-uzeit extension INTO path .

CALL FUNCTION 'FTP_R3_TO_SERVER'"For Creating a file from SAP R3 to FTP Server
EXPORTING
handle = mi_handle
fname = path
character_mode = 'X'
TABLES
text = it_final"Final Internal table to be written to the text file in the FTP Server's Directory
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
*** Here getting ,sy-subrc = 2 ***************


ELSE."When FTP connection Fails
WRITE: / 'Error in FTP Command'.
ENDIF.

CALL FUNCTION 'FTP_DISCONNECT'"For Disconnecting the connected FTP Session
EXPORTING
handle = mi_handle
EXCEPTIONS
OTHERS = 1.