Download file from application server (AL11) abap - aidamate13/AMS-Knowledge-Base GitHub Wiki
Use OPEN DATASET to Read the File from Application Server: The OPEN DATASET command is used to open the file for reading from the application server.
Use READ DATASET to Read Data into an Internal Table: The READ DATASET command reads the data from the opened file into an internal table.
Use GUI_DOWNLOAD to Download the File to the Local PC: The GUI_DOWNLOAD function module is used to download the data from the internal table to the local machine.
DATA: lv_file_name TYPE string,
lv_file_path TYPE string,
lv_line TYPE string,
lt_file_data TYPE TABLE OF string,
lv_filename TYPE string,
lv_fullpath TYPE string.
" File path on the application server (e.g., from AL11)
lv_file_path = '/usr/sap/trans/data/YOUR_FILENAME.TXT'.
" Open the dataset on the application server for reading
OPEN DATASET lv_file_path FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc = 0.
" Read each line from the dataset
DO.
READ DATASET lv_file_path INTO lv_line.
IF sy-subrc <> 0.
EXIT.
ENDIF.
" Append each line to the internal table
APPEND lv_line TO lt_file_data.
ENDDO.
" Close the dataset after reading
CLOSE DATASET lv_file_path.
ELSE.
" Handle the error if the file cannot be opened
WRITE: / 'Error opening file on application server'.
EXIT.
ENDIF.
" Set the name for the local file
lv_filename = 'C:\temp\YOUR_LOCAL_FILENAME.TXT'.
" Download the internal table to the local file
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = lv_filename
filetype = 'ASC'
TABLES
data_tab = lt_file_data
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
others = 22.
IF sy-subrc <> 0.
WRITE: / 'Error downloading file to local machine'.
ENDIF.