✒️ABAP Los archivos locales y el upload de datos
ABAP Los archivos locales y el upload de datos
Handling Local Files
ABAP provides a set of highly useful methods for handling local files located on our PC through the CL_GUI_FRONTEND_SERVICES class.
- To view all available methods in the class, edit an ABAP program and press the Pattern(Model) button.
- Select the ABAP Object Patterns option and press continue.
- On the next screen, enter the class CL_GUI_FRONTEND_SERVICES.
- Press F4 on Method, and you'll see a screen with all the class methods.
We will explore the following methods in detail:
- FILE_OPEN_DIALOG: Displays the navigation screen allowing the user to select a file from their PC directory.
- GUI_DOWNLOAD: Downloads data from programs to files on the PC.
- GUI_UPLOAD: Uploads data from PC files to our programs.
Another way to view the content of the CL_GUI_FRONTEND_SERVICES class is by entering the Class Builder, which is the SE24 transaction.
Searching for Files on the PC
To search for local files in our PC directory, we will use the FILE_OPEN_DIALOG method.
*--------------------------------------------------------------------*
* Selection Screen
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK z2 WITH FRAME TITLE TEXT-007.
* Input File Parameter
PARAMETERS: p_path LIKE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK z2.
*--------------------------------------------------------------------*
START-OF-SELECTION.
*--------------------------------------------------------------------*
* Open File Window
PERFORM open_file_window.
*&---------------------------------------------------------------------*
*& Form OPEN_FILE_WINDOW
*&---------------------------------------------------------------------*
* This form opens a dialog window for selecting files.
*----------------------------------------------------------------------*
FORM open_file_window.
DATA: tl_filetable TYPE filetable,
wl_file_table TYPE file_table,
vl_rc TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Test Window Title'
default_filename = '*'
CHANGING
rc = vl_rc
file_table = tl_filetable.
READ TABLE tl_filetable INDEX 1 INTO wl_file_table.
IF sy-subrc EQ 0.
MOVE wl_file_table TO p_path.
ENDIF.
ENDFORM. " OPEN_FILE_WINDOW"
Key parameters of the method:
- WINDOW_TITLE: Assigns a title to the search window.
- DEFAULT_FILENAME: Contains the name of the file we are looking for on our PC. Using an asterisk allows us to see all files in the directories.
- FILE_TABLE: Contains the path and name of the selected file.
Like functions, the FILE_OPEN_DIALOG method has defined exceptions, each associated with a description covering possible errors that may occur when executing the method.
It is advisable to use the FILE_OPEN_DIALOG method in ABAP programs dealing with local files, as it significantly facilitates interaction between the user and the program, preventing data typing errors.
Reading Data from Local Files
To read data from local files into the internal tables of our programs, we will use the GUI_UPLOAD method.
The double brackets used on the right of the internal table name lifted using the upload method are a requirement of the method and simply fulfill a syntax requirement of the method.
*----------------------------------------------------------------------*
* Data Declaration
*----------------------------------------------------------------------*
* User Table
DATA: BEGIN OF ti_file OCCURS 0,
dni(8) TYPE c, " DNI
fullname(35) TYPE c, " Fullname
status(1) TYPE c, " Status
birthdate(8) TYPE c, " Date of Birth
END OF ti_file.
DATA: wa_file LIKE LINE OF ti_file.
*--------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
*--------------------------------------------------------------------*
* Open File Window
PERFORM open_file_window.
*&---------------------------------------------------------------------*
*& Form UPLOAD_INPUT_FILE
*&---------------------------------------------------------------------*
* This form processes the input file.
*----------------------------------------------------------------------*
FORM upload_input_file.
DATA: vl_file TYPE string.
IF p_path IS INITIAL.
* The file path is blank
MESSAGE s000(z_prueba) WITH TEXT-003.
ELSE.
vl_file = p_path.
* Open the input file for reading
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = vl_file
filetype = 'ASC'
has_field_separator = ''
CHANGING
data_tab = ti_file[]
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc NE 0.
* Error uploading the input file
MESSAGE e000(z_prueba) WITH TEXT-004.
ENDIF.
IF ti_file[] IS INITIAL.
* The input file does not contain records
MESSAGE s000(z_prueba) WITH TEXT-005.
ENDIF.
ENDIF.
ENDFORM. " UPLOAD_INPUT_FILE"
Key parameters of the method:
- FILENAME: Contains the directory and name of the local file we are working with.
- FILETYPE: Contains the type or extension of the local file. Supported types include BIN for binary files, ASC for ASCII files, DAT for Excel files (columns separated by tabs, lines separated by line breaks), and WK1 for Excel and Lotus files.
Note that to upload an Excel file from our PC, the file should not have the .xls extension. Instead, modify the extension to .csv for proper reading by the upload function.
- HAS_FIELD_SEPARATOR: Indicates that the local file has separators.
- DATA_TAB: The internal table where we store the data from the local file.
This method has defined exceptions with descriptions covering possible errors that may occur when executing the method. In some ABAP programs, functions like WS_UPLOAD and UPLOAD are used. However, it is not advisable to use these functions as they are currently considered obsolete by SAP.
E.g
TestFile:
21567432A23071990Jaime Gomez
31987623I15061988Jorge Gomez
27982134A02121996Angelica Ramirez
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_21
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_21.
*----------------------------------------------------------------------*
* Data Declaration
*----------------------------------------------------------------------*
* User Table
DATA: BEGIN OF ti_file OCCURS 0,
dni(8) TYPE c, " DNI
status(1) TYPE c, " Status
birthdate(8) TYPE c, " Date of Birth
fullname(35) TYPE c, " Fullname
END OF ti_file.
DATA: wa_file LIKE LINE OF ti_file.
*--------------------------------------------------------------------*
* Selection Screen
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK z2 WITH FRAME TITLE TEXT-007.
* Input File Parameter
PARAMETERS: p_path LIKE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK z2.
*--------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
*--------------------------------------------------------------------*
* Call open window
PERFORM open_file_window.
*--------------------------------------------------------------------*
START-OF-SELECTION.
*--------------------------------------------------------------------*
* Upload input data
PERFORM upload_input_file.
*&---------------------------------------------------------------------*
*& Form OPEN_FILE_WINDOW
*&---------------------------------------------------------------------*
* This form opens a dialog window for selecting files.
*----------------------------------------------------------------------*
FORM open_file_window.
DATA: tl_filetable TYPE filetable,
wl_file_table TYPE file_table,
vl_rc TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Test Window Title'
default_filename = '*'
CHANGING
rc = vl_rc
file_table = tl_filetable.
READ TABLE tl_filetable INDEX 1 INTO wl_file_table.
IF sy-subrc EQ 0.
MOVE wl_file_table TO p_path.
ENDIF.
ENDFORM. " OPEN_FILE_WINDOW"
*&---------------------------------------------------------------------*
*& Form UPLOAD_INPUT_FILE
*&---------------------------------------------------------------------*
* This form processes the input file.
*----------------------------------------------------------------------*
FORM upload_input_file.
DATA: vl_file TYPE string.
IF p_path IS INITIAL.
* The file path is blank
MESSAGE s000(z_prueba) WITH TEXT-003.
ELSE.
vl_file = p_path.
* Open the input file for reading
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = vl_file
filetype = 'ASC'
has_field_separator = ''
CHANGING
data_tab = ti_file[]
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc NE 0.
* Error uploading the input file
MESSAGE e000(z_test) WITH TEXT-004.
ENDIF.
IF ti_file[] IS INITIAL.
* The input file does not contain records
MESSAGE s000(z_test) WITH TEXT-005.
ELSE.
LOOP AT ti_file INTO DATA(wa_file_record).
WRITE:/ 'DNI:', wa_file_record-dni, ' ',
'FullName:', wa_file_record-fullname, ' ',
'Status:', wa_file_record-status, ' '.
ULINE.
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM. " UPLOAD_INPUT_FILE"
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 99 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez