Application Development 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: 

FTP_CONNECT

Former Member
0 Kudos

I want to FTP a file. Can anyone explain me HANDLE . Also do I need DATA = MTAB_DATA. I am not sure about the purpose of this internal table

  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      USER            = 'userid'
      PASSWORD        = MC_PASSWORD
      HOST            = 'servername'
      RFC_DESTINATION = 'SAPFTP'
    IMPORTING
      HANDLE          = MI_HANDLE
    EXCEPTIONS
      NOT_CONNECTED   = 1
      OTHERS          = 2.

1 ACCEPTED SOLUTION

ferry_lianto
Active Contributor
0 Kudos

Hi,

Handle is refer to FTP session.

Are you referring MTAB_DATA to this sample codes.

If you are, the you need to declare MTAB_DATA which will be used to receive the result from FM FTP_COMMAND. This example will return list of directory for the current FTP session.


CALL FUNCTION 'FTP_COMMAND'
EXPORTING
HANDLE = MI_HANDLE
COMMAND = 'dir'
TABLES
DATA = MTAB_DATA
EXCEPTIONS
TCPIP_ERROR = 1
COMMAND_ERROR = 2
DATA_ERROR = 3
OTHERS = 4.

IF SY-SUBRC = 0.
LOOP AT MTAB_DATA.
WRITE: / MTAB_DATA.
ENDLOOP.
ELSE.
* do some error checking.
ENDIF.

Regards,

Ferry Lianto

4 REPLIES 4

ferry_lianto
Active Contributor
0 Kudos

Hi,

Handle is refer to FTP session.

Are you referring MTAB_DATA to this sample codes.

If you are, the you need to declare MTAB_DATA which will be used to receive the result from FM FTP_COMMAND. This example will return list of directory for the current FTP session.


CALL FUNCTION 'FTP_COMMAND'
EXPORTING
HANDLE = MI_HANDLE
COMMAND = 'dir'
TABLES
DATA = MTAB_DATA
EXCEPTIONS
TCPIP_ERROR = 1
COMMAND_ERROR = 2
DATA_ERROR = 3
OTHERS = 4.

IF SY-SUBRC = 0.
LOOP AT MTAB_DATA.
WRITE: / MTAB_DATA.
ENDLOOP.
ELSE.
* do some error checking.
ENDIF.

Regards,

Ferry Lianto

Former Member
0 Kudos

I still dont understand why i need to find list of directory. I simply need to put a txt file onto another ftp server

0 Kudos

Hi Megan.

I finally got it right. You need to first download your file then use command put to place the file on the remote server. My code looks like this. 2 performs one to create the file and then the second will place it on the destination server. Hopes will help you. The put command and docid is that of the file in the local folder that you change using the command lsd p_path.

Regards,

Marius

FORM download USING p_file_name.

DATA: w_lfile LIKE rlgrap-filename,

w_lf(5) TYPE c,

w_message TYPE string,

w_string(500) TYPE c.

ddocid = p_file_name.

  • Create file on Source

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = 'Create File on Local server'.

CONCATENATE p_path p_file_name INTO w_lfile.

IF sy-batch = 'X'.

  • Opens file for output in text mode

OPEN DATASET w_lfile

FOR OUTPUT IN TEXT MODE ENCODING DEFAULT

MESSAGE w_message.

IF sy-subrc NE 0.

  • RAISE INVALID_FILEPATH.

MESSAGE e001(00) WITH w_message.

ENDIF.

  • Transfers each line of the internal table to the file

LOOP AT it_output.

w_string = it_output-line.

MOVE cl_abap_char_utilities=>cr_lf TO w_lf.

CONCATENATE w_string w_lf INTO w_string.

TRANSFER w_string TO w_lfile NO END OF LINE.

ENDLOOP.

CLOSE DATASET w_lfile.

ELSE.

CALL METHOD cl_download->gui_download

EXPORTING

filename = p_file_name

filetype = 'ASC'

CHANGING

data_tab = it_output[]

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

no_authority = 5

unknown_error = 6

header_not_allowed = 7

separator_not_allowed = 8

filesize_not_allowed = 9

header_too_long = 10

dp_error_create = 11

dp_error_send = 12

dp_error_write = 13

unknown_dp_error = 14

access_denied = 15

dp_out_of_memory = 16

disk_full = 17

dp_timeout = 18

file_not_found = 19

dataprovider_exception = 20

control_flush_error = 21

not_supported_by_gui = 22

error_no_gui = 23.

ENDIF.

ENDFORM. " download

FORM ftp_file USING p_file_name.

DATA: w_cmd1(120),

w_cmd2(120),

w_cmd3(120),

w_cmd4(120),

w_cmd5(120).

  • Change to ascii process status

cmd = 'ascii'.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

handle = w_handle

command = cmd

compress = 'N'

TABLES

data = result

EXCEPTIONS

command_error = 1

tcpip_error = 2.

  • Change local path to where file was downloaded

CONCATENATE 'lcd' p_path INTO cmd SEPARATED BY ' '.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

handle = w_handle

command = cmd

compress = 'N'

TABLES

data = result

EXCEPTIONS

command_error = 1

tcpip_error = 2.

  • Create file on Source

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = 'Placing file on Destination server'.

  • Put new file on remote system

CONCATENATE 'put' ddocid INTO cmd SEPARATED BY ' '.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

handle = w_handle

command = cmd

compress = 'N'

TABLES

data = result

EXCEPTIONS

command_error = 1

tcpip_error = 2.

ENDFORM. " ftp_file

Former Member
0 Kudos

Hi Megan Flores,

In Function moduel FTP_CONNECT, we pass the User name and the Scambled password with Server name and the Destination, adn we recive the message from the FTP whether we successfully conected or not,

in the field MI_HANDLE, you will get whehter the FTP is success or not, it is equals to SY-SUBRC in Namal ABAP.

Aftter that you need to call the Function module FTP_COMMAND, to pass a command to the FTP to place a Txt fiel over there

Regards

Sudheer