GUI_DOWNLOAD¶
Transfer data from the SAP application server to the user's PC — the standard way to write files to the local machine from ABAP.
Purpose¶
GUI_DOWNLOAD writes an internal table to a file on the SAP GUI client's local filesystem. It supports text (ASC), binary (BIN), and delimited formats. The file dialog can be shown automatically or the path supplied directly.
For the reverse direction (upload from PC to server), use GUI_UPLOAD.
Signature (key parameters)¶
| Parameter | Direction | Type | Description |
|---|---|---|---|
FILENAME |
Importing | STRING |
Local file path on the client PC |
FILETYPE |
Importing | CHAR10 |
'ASC' = text, 'BIN' = binary, 'DAT' = tab-delimited |
APPEND |
Importing | CHAR1 |
'X' = append to existing file |
WRITE_FIELD_SEPARATOR |
Importing | CHAR1 |
'X' = add tab between fields (for DAT) |
CODEPAGE |
Importing | ABAP_ENCODING |
Target encoding (e.g. '4110' = UTF-8) |
DATA_TAB |
Tables | STANDARD TABLE |
Data to write |
FILELENGTH |
Exporting | INT4 |
Bytes written |
Example — download internal table as text file¶
DATA lt_lines TYPE STANDARD TABLE OF string.
APPEND 'Header line 1;Value A;Value B' TO lt_lines.
APPEND 'Header line 2;Value C;Value D' TO lt_lines.
DATA lv_path TYPE string VALUE 'C:\temp\export.csv'.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = lv_path
filetype = 'ASC'
TABLES
data_tab = lt_lines
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
OTHERS = 14.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Example — download binary file (PDF, ZIP)¶
DATA: lv_xstring TYPE xstring, " your binary data
lt_binary TYPE STANDARD TABLE OF x255,
lv_offset TYPE i VALUE 0,
lv_total TYPE i,
lv_chunk TYPE i,
ls_line TYPE x255.
lv_total = xstrlen( lv_xstring ).
WHILE lv_offset < lv_total.
CLEAR ls_line.
lv_chunk = lv_total - lv_offset.
IF lv_chunk > 255. lv_chunk = 255. ENDIF.
ls_line = lv_xstring+lv_offset(lv_chunk).
APPEND ls_line TO lt_binary.
lv_offset = lv_offset + lv_chunk.
ENDWHILE.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:\temp\report.pdf'
filetype = 'BIN'
TABLES
data_tab = lt_binary
EXCEPTIONS
OTHERS = 1.
Example — show file save dialog first¶
DATA: lv_path TYPE string,
lv_dir TYPE string,
lv_full TYPE string,
lv_fname TYPE string VALUE 'export.csv',
lv_rc TYPE i,
lt_lines TYPE STANDARD TABLE OF string.
APPEND 'Col1;Col2;Col3' TO lt_lines.
APPEND 'Val1;Val2;Val3' TO lt_lines.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = 'Save export file'
default_file_name = lv_fname
default_extension = 'csv'
CHANGING
filename = lv_path
path = lv_dir
fullpath = lv_full
user_action = lv_rc
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0 AND lv_full IS NOT INITIAL.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING filename = lv_full filetype = 'ASC'
TABLES data_tab = lt_lines
EXCEPTIONS OTHERS = 1.
ENDIF.
Common pitfalls¶
NO_BATCH exception in background jobs
GUI_DOWNLOAD requires an active SAP GUI connection. It raises NO_BATCH when called from a background job. Use OPEN DATASET for application server file operations in background processing instead.
Path is on the client, not the server
The file path (FILENAME) refers to the user's PC filesystem — not the SAP application server. To write to the application server use OPEN DATASET ... FOR OUTPUT.
- Encoding: Code page
'4110'= UTF-8. Code page'1100'= UTF-16BE. IfCODEPAGEis blank, the system default is used. - Tab-delimited output: Set
FILETYPE = 'DAT'andWRITE_FIELD_SEPARATOR = 'X'to automatically insert tabs between structure fields — useful for structured internal tables.
See also¶
CL_GUI_FRONTEND_SERVICES— file dialog, directory listing,GUI_UPLOAD- Read and write files tutorial — server-side file I/O with OPEN DATASET
- Transaction AL11 — application server directory browser