11.3    File Interfaces

In addition to storing data in the database layer, you can save data in a persistent form in the application and presentation layers. Data is stored as sequential files in these layers—for example, as spreadsheets. In this section, you’ll learn about working with files in the presentation application layers.

We’ll first look at how sequential files can be saved to the application server directories from the ABAP program and how to read the saved files to be processed in ABAP programs in Section 11.3.1. Then, in Section 11.3.2, we’ll discuss exporting and importing files from and to the presentation layer.

11.3.1    Working with Files in the Application Server

We use different ABAP statements to transfer data to a file stored in the application server directory. We typically perform two kinds of operations with files stored in the application server directory:

  1. Read the file from the application server directory to an ABAP program (inbound interface)
  2. Transfer the file from the ABAP program to the application server directory (outbound interface)

In an inbound interface, the data is read from the file and transferred to an internal table for further processing in the ABAP program. Conversely, in an outbound interface, the data from an internal table in the ABAP program is transferred to the file in the application server.

The following ABAP statements are used to read and write the files in the application server:

The file names are operating system-dependent because they’re physical names. We recommend using platform-independent file names to make your programs portable.

To make your file names platform-independent, use logical file names and paths. These logical file names and paths are linked to physical file names and paths using special tables in customizing that can be maintained using Transaction FILE. The physical file name from a logical file name is generated using the FILE_GET_NAME function module.

Using logical file names and paths also enhances security.

11.3.2    Working with Files in the Presentation Layer

Sometimes, users may want to save data locally to their workstations or upload data to a program for processing. For example, a user may want to download the report output as a PDF file or upload a spreadsheet with material data to be posted in the database. In such scenarios, we can use the CL_GUI_FRONTEND_SERVICES global class to work with the files in the presentation layer.

Among other methods, the class provides the GUI_UPLOAD and GUI_DOWNLOAD static methods to upload or download data from or to the user’s workstation. The class documentation available for these methods in Class Builder explains their usage clearly.

To download the file to the presentation layer, the file path, file type (binary or ASCII), and internal table that contains the data are passed to the GUI_DOWNLOAD method.

To upload a file, an internal table is defined with the number of fields matching the number of columns in the file. This table is then passed to the GUI_UPLOAD method along with the file path. The data will be uploaded from the file to the supplied internal table.

Listing 11.13 shows sample code using the GUI_UPLOAD method.

TYPES : BEGIN OF ty_file,
field1 TYPE char20,
field2 TYPE char30,
field3 TYPE char10,
END OF ty_file.
DATA : it_file TYPE STANDARD TABLE OF ty_file.
PARAMETERS p_file type string.

CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = p_file
filetype = 'ASC'
has_field_separator = SPACE
CHANGING
data_tab = it_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 <> 0.
* Implement suitable error handling here
ENDIF.

Listing 11.13    GUI Upload

In this section, we looked at how to save data persistently in the application and presentation layers. In the next section, we’ll discuss what data clusters are and how to export and import data by using them.