Uploading files to the SAP ERP application server and reading data from them is a function that is very often requested by users. Today, you can learn how to implement this very easily using the SAP Control Framework.
Uploading files is quite simple if you know how to do it. Once again (and not for the last time), we encounter the class CL_GUI_FRONTEND_SERVICES. With two simple method calls, it allows us to select a file from the current desktop and upload it to the SAP application server, reading it into a variable.
Let’s consider the setup first. I’ve developed a small demo report that has exactly one parameter: the name of a file, or rather, a path to a file on the current user’s machine. Using this path, I will then read the file into a variable. Let’s look at how to retrieve that path first.
PARAMETERS p_fname TYPE char128 OBLIGATORY. * When the user presses F4, show the file open dialog AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname. CALL METHOD cl_gui_frontend_services=>file_open_dialog CHANGING file_table = gt_filetable rc = gv_return EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. * Read the file name table and move it to the param READ TABLE gt_filetable INTO gv_filetable INDEX 1. p_fname = gv_filetable.
You can see that there is nothing special about the parameter – it’s a simple character variable. The knack is the usage of AT SELECTION-SCREEN ON VALUE-REQUEST. Once the user presses F4, he will be shown a file selection dialog. After he has picked a file, the according path is moved from the class method return value to the parameter so the user gets some feedback.
The next step is to actually read the file. This should happen once the report is executed, so it is done at the START-OF-SELECTION event.
START-OF-SELECTION. * The gui_upload method wants the filename as string gv_filename = p_fname. CALL METHOD cl_gui_frontend_services=>gui_upload EXPORTING filename = gv_filename CHANGING data_tab = gt_line 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.
Before the file is actually uploaded, the file path needs to be moved into a string variable. After that, we can simply call the GUI_UPLOAD method of class CL_GUI_FRONTEND_SERVICES to read the data into an internal table. You will now have all the data from the file available in the variable gt_line. You could now loop over the table as usual and process the data line by line. Neat!
The class CL_GUI_FRONTEND_SERVICES is part of the SAP Control Framework, a library that allows ABAP developers to interact with desktop applications and use native desktop functionality. I will write a few more posts about this in the next few days to explore some more possibilities. As usual, you can find today’s demo program on the next page.