Home »
Upload Binary Files to XSTRING with ABAP GUI_UPLOAD

Last updated:
To read a local file in SAP GUI, open a file dialog, upload the file in binary mode and convert the table to an xstring. Pass the file length to the conversion; without it, the padding bytes of the last row end up in your file.
DATA lt_files TYPE filetable.
DATA lv_rc TYPE i.
DATA lv_action TYPE i.
DATA lt_binary TYPE solix_tab.
DATA lv_length TYPE i.
cl_gui_frontend_services=>file_open_dialog(
EXPORTING window_title = 'Select files'
multiselection = abap_true
CHANGING file_table = lt_files
rc = lv_rc
user_action = lv_action
EXCEPTIONS OTHERS = 1 ).
IF sy-subrc <> 0 OR lv_rc < 0
OR lv_action = cl_gui_frontend_services=>action_cancel.
RETURN.
ENDIF.
LOOP AT lt_files INTO DATA(ls_file).
DATA(lv_path) = CONV string( ls_file-filename ).
CLEAR: lt_binary, lv_length.
cl_gui_frontend_services=>gui_upload(
EXPORTING filename = lv_path
filetype = 'BIN'
IMPORTING filelength = lv_length
CHANGING data_tab = lt_binary
EXCEPTIONS OTHERS = 1 ).
IF sy-subrc <> 0.
WRITE: / 'Upload failed:', lv_path.
CONTINUE.
ENDIF.
DATA(lv_content) = cl_bcs_convert=>solix_to_xstring(
it_solix = lt_binary
iv_size = lv_length ).
DATA(lv_size) = xstrlen( lv_content ).
WRITE: / lv_path, lv_size.
ENDLOOP.
Good to know
GUI_UPLOADtalks to SAP GUI. It does not work in background jobs or in OData services.- Text files need a decision about encoding. Upload them as binary and convert with the correct code page, see STRING and XSTRING conversion.
- Do not trust the file extension. Check size and content before processing.
- Values in a CSV file can be split with the CSV parser.