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

Command Error from FTP_R3_TO_SERVER

thangam_perumal
Contributor
8,019

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.

1 ACCEPTED SOLUTION
Read only

thangam_perumal
Contributor
4,009

Finally solved the issue.

After connecting FTP server, I set passive mode on.

CALL FUNCTION 'FTP_COMMAND'
       EXPORTING
         handle        = mi_handle
         command       = 'set passive on'
       TABLES
         data          = result
       EXCEPTIONS
         command_error = 1
         tcpip_error   = 2.

4 REPLIES 4
Read only

thangam_perumal
Contributor
0 Likes
4,009

Hi,

     Anyone help me out.

Read only

Sandra_Rossi
Active Contributor
0 Likes
4,009

Could you first do the basic verifications as explained in note 93042 - Problems with SAPFTP -> https://service.sap.com/sap/support/notes/93042

Read only

thangam_perumal
Contributor
4,010

Finally solved the issue.

After connecting FTP server, I set passive mode on.

CALL FUNCTION 'FTP_COMMAND'
       EXPORTING
         handle        = mi_handle
         command       = 'set passive on'
       TABLES
         data          = result
       EXCEPTIONS
         command_error = 1
         tcpip_error   = 2.

Read only

0 Likes
4,009

Is this the correct one?