Register Login

File Handling - Presentation and Application Server

Updated May 18, 2018

File Transfer

  • The runtime environment, implemented on the application server, executes the ABAP programs. ABAP supports file transfer on the application server and on the front-end computers.
  • The processing of the sequential files on the application server is supported by using ABAP language elements.
  • You process sequential files with the ABAP commands OPEN DATASET, READ DATASET (read records), TRANSFER (write records), and CLOSE DATASET.
  • The file interface on the presentation server is implemented by function modules.
  • Before data records can be written to a sequential file or read from a file, the file has to be opened.
  • After processing the file is closed again.
  • The data is transferred from the application server to the database server with batch input (BI), Call Transaction (CT) or Direct Input (DI).

Some of the Commonly Used File-types

  • Tab delimited
  • Comma separated
  • Fixed length etc.

Presentation Server File Handling

Upload and Download

  • Using the function module DOWNLOAD you can transfer the contents of an internal table to a local file.
  • Using the function module UPLOAD you can transfer the contents from a local sequential file into an internal table.
  • Enter the whole file path and name of the local file (e.G. '/tmp/myfile' for a Unix file and e.G. 'C: MYFILE.TXT' for a PC file).
  • The presentation server must know the directory.
  • Customers can choose an appropriate file name.
  • The system asks you for the file name and file type.
  • The data is then converted into the appropriate file type.

Read here How to read an Excel file from presentation server?

Important Function Groups:

  • SFES (Frontend Services)
  • GRAP (Calling Up Graphics / File transfer)

Reading Data From Presentation Server

  • For the function module UPLOAD you need an internal table for the data transfer. You can define this table to fit your data structure at the start of the program.
  • If you want, you can specify default values for the file name and file type and enter a title for the file dialog.
  • If the function module UPLOAD stores a file under DOS, the export parameter CODEPAGE must be set to 'IBM‘. The parameter has no other meaning.

WS_UPLOAD

IMPORT PARAMETERS

  • CODEPAGE DEFAULT SPACE
  • FILENAME LIKE RLGRAP-FILENAME DEFAULT SPACE
  • FILETYPE LIKE RLGRAP-FILETYPE DEFAULT 'ASC'
  • HEADLEN DEFAULT SPACE
  • LINE_EXIT DEFAULT SPACE
  • TRUNCLEN DEFAULT SPACE
  • USER_FORM DEFAULT SPACE
  • USER_PROG DEFAULT SPACE
  • DAT_D_FORMAT TYPE DATEFORMAT DEFAULT SPACE

EXPORT PARAMETERS

  • FILELENGTH

TABLES

  • DATA_TAB (Mandatory)

EXCEPTIONS

  • CONVERSION_ERROR
  • FILE_OPEN_ERROR
  • FILE_READ_ERROR
  • INVALID_TYPE
  • NO_BATCH
  • UNKNOWN_ERROR
  • INVALID_TABLE_WIDTH
  • GUI_REFUSE_FILETRANSFER
  • CUSTOMER_ERROR

Writing Data to Presentation Server

  • For the function module DOWNLOAD you need an internal table for the data transfer. You can define this table to fit your data structure and then fill it with your data.
  • With the parameter MODE you can decide which write mode is used ('A' for extend file, ' ' for create new file).
  • If you want, you can specify default values for the file name and file type and enter a title for the file dialog.
  • The IMPORT parameters specify the actual values entered by the user for the file name and file type and also the number of bytes transferred.
  • If the function module DOWNLOAD stores a file under DOS, the export parameter CODEPAGE must be set to 'IBM‘. The parameter has no other meaning.
  • All EXPORTING parameters are optional.
  • If a binary file is to be created, the file length must be specified. In this case the transfer table must consist of one column of type X (hexadecimal numbers).

WS_DOWNLOAD

IMPORT PARAMETERS

  • CODEPAGE DEFAULT SPACE
  • BIN_FILESIZE DEFAULT SPACE
  • CODEPAGE DEFAULT SPACE
  • FILENAME LIKE RLGRAP-FILENAME DEFAULT SPACE
  • FILETYPE LIKE RLGRAP-FILETYPE DEFAULT 'ASC'
  • MODE DEFAULT SPACE
  • WK1_N_FORMAT DEFAULT SPACE
  • WK1_N_SIZE DEFAULT SPACE
  • WK1_T_FORMAT DEFAULT SPACE
  • WK1_T_SIZE DEFAULT SPACE
  • COL_SELECT DEFAULT SPACE
  • COL_SELECTMASK DEFAULT SPACE
  • NO_AUTH_CHECK TYPE C DEFAULT SPACE

EXPORT PARAMETERS

  • FILELENGTH

TABLES

  • DATA_TAB (Mandatory)
  • FIELDNAMES OPTIONAL

EXCEPTIONS

  • FILE_OPEN_ERROR
  • FILE_WRITE_ERROR
  • INVALID_FILESIZE
  • INVALID_TYPE
  • NO_BATCH
  • UNKNOWN_ERROR
  • INVALID_TABLE_WIDTH
  • GUI_REFUSE_FILETRANSFER
  • CUSTOMER_ERROR

Exercise – Presentation Server

  • Create a tab delimited file in the presentation server having the following fields:
    • Sales document (VBAP-VBELN)
    • Sales document item (VBAP-POSNR)
    • Material number (VBAP-MATNR)
    • Material group (VBAP-MATKL)
    • Sales order item description (VBAP-ARKTX)
  • Upload the data from the file to an internal table using F4 help and display in the report.
  • Create two new files in the pc with the same data:
    • Tab delimited (‘DOWNLOAD.Txt’)
    • Excel file (‘DOWNLOAD.XLS’)

Read here Presentation and Application Servers in SAP

Application Server File Handling

Read / Write File

  • A file processing program must define the required structures for the data records using a TABLES or DATA statement. These structures are used as program-internal work areas for the data records.
  • The OPEN statement is used to begin file processing and open the source and target sequential files for reading or writing.
  • Reading: The READ DATASET statement is used to read the records from the source file into the data structures for later processing in the program.
  • Writing: TRANSFER statements are used to transfer the filled structures to the target file.
  • The CLOSE DATASET statement is used to end file processing and close the sequential files.

Open File

  • You can explicitly open a file using the command OPEN DATASET .
  • If the file can be opened, SY-SUBRC is set to 0, otherwise it is set to 8. Any errors are ignored.
  • We recommend, for reasons of clarity, that you open the file explicitly. Using the OPEN DATASET statement allows you to test whether an error has occurred when you open the file (SY-SUBRC = 8). If you do not explicitly open the file, a READ DATASET or TRANSFER command will attempt to open the file using the default mode. In this case, if an open error occurs, you cannot trap the error and the program will terminate processing.

Open Dataset

Basic form 1

OPEN DATASET dsn.

Additions:

  1. ... FOR INPUT
  2. ... FOR OUTPUT
  3. ... FOR APPENDING
  4. ... IN BINARY MODE
  5. ... IN TEXT MODE
  6. ... AT POSITION p
  7. ... TYPE ctrl
  8. ... MESSAGE mess
  9. ... FILTER f

Transfer Data

  • The TRANSFER command is used to write a data record into a sequential file.
  • A field or structure would be filled wit data before each TRANSFER command.
  • can be a field or a structure.
  • The execution of the TRANSFER command depends on the mode: -
  • - Binary mode: Appends the length of the field or the structure to the file - Text mode: Writes a new record to the file
  • If the specified file is not opened, TRANSFER attempts to open the file FOR OUTPUT (IN BINARY MODE or with the further specifications of the last OPEN command for this file). If this does not work, a runtime error results.
  • Any errors will result in the program terminating.
  • The additional parameter LENGTH permits an explicit length specification in bytes. In this case the exact number of characters specified in are transferred. If the structure is shorter, it is padded (in text mode with blank characters and in binary mode with hexadecimal zeros). If the structure is longer, it is truncated.

Transfer

Basic form 1

TRANSFER f TO dsn.

Additions:

  1. ... LENGTH len

Example

DATA w_rec(80).

TRANSFER w_rec TO '/scma/test.dat'.

Read Data

  • With READ DATASET you can read a record from a sequential file into a field or structure.
  • Possible structures are field strings or table work areas.
  • The execution of the READ DATASET command depends on the mode: -
  • - Binary mode: Reads the structure length

- Text mode: Reads one record of data

  • If the specified file is not opened, READ DATASET attempts to open the file (IN BINARY MODE FOR INPUT) or with the additions of the last OPEN DATASET command for this file).
  • If the file end has been reached, SY-SUBRC is set to 4, otherwise it is set to 0. If the file cannot be opened, SY-SUBRC is set to 8. Any errors will result in the program terminating.
  • READ DATASET, like TRANSFER, does not execute any implicit conversions. The data is imported as it has been written.
  • The READ DATASET command together with the additional parameter LENGTH returns the length of the imported file record into the field <len>.

Read Dataset

Basic form

READ DATASET dsn INTO f.

Addition:

  1. ...LENGTH len

Example

DATA: w_dsn(20) VALUE '/scma/test.dat',

w_rec(80).

OPEN DATASET w_dsn.

IF sy-subrc = 0.

DO.

READ DATASET w_dsn INTO w_rec.

IF sy-subrc <> 0.

EXIT.

ELSE.

WRITE / w_ rec.

ENDIF.

ENDDO.

ENDIF.

CLOSE DATASET w_dsn.

Close / Delete File

  • The command CLOSE DATASET closes a sequential file. Errors are ignored the same as with OPEN DATASET.
  • Within a program when the screen is changed all open files are shut and opened again with their old status when processing is started again.
  • All files are closed when the program is exited.
  • We recommend, for reasons of clarity, that you open and close the file directly. OPEN DATASET has also the advantage that if an error does occur when you open the file (SY-SUBRC = 8), it will not result in a termination.
  • A file is not implicitly closed, if READ DATASET reaches the end of the file.
  • With the command DELETE DATASET you can physically delete a sequential file. The file will then not longer exist in the operating system. Once it has been deleted, SY-SUBRC is set to 0.
  • The current file status can be displayed in debugging.

Delete Dataset

Basic form

DELETE DATASET w_dsn.

Important Commands

  • OPEN DATASET

- for Input, Output

  • READ DATASET

- for Input

  • TRANSFER

- Create file

  • DELETE DATASET

- Delete file

Exercise – Application Server

  • Create a text file in the application server having the following fields:
    • Sales document (VBAP-VBELN)
    • Sales document Item (VBAP-POSNR)
    • Material number (VBAP-MATNR)
    • Material group (VBAP-MATKL)
    • Sales order item description (VBAP-ARKTX)
    • Where Sales document lies between 10 and 100.
  • Read the data from the created file and display a report.

The File Monitor AL11

Logical File Name

  • A common use of logical file names is when archiving of R/3 application data using the transaction "SARA". However, logical file names can also be useful for external data transfer programs.
  • Using the platform-independent, logical file name you can specify the file name and menu path under which files are to be created. According to the operating system used, the physical paths and file names specified in Customizing (transaction "FILE") are used.
  • The portability of programs can be implemented using a function module (FILE_GET_NAME).
  • You can create logical paths and files using the Customizing transaction "FILE".
  • Platform-specific physical path must contain the reserved word as the placeholder for the file name. It may also contain other reserved words - see the documentation (F1 help).

KD_GET_FILENAME_ON_F4

IMPORT PARAMETERS

  • PROGRAM_NAME LIKE SY-REPID DEFAULT SYST-REPID
  • DYNPRO_NUMBER LIKE SY-DYNNR DEFAULT SYST-DYNNR
  • FIELD_NAME DEFAULT SPACE
  • STATIC DEFAULT SPACE
  • MASK TYPE C DEFAULT SPACE

CHANGING PARAMETERS:

  • FILE_NAME LIKE RLGRAP-FILENAME

EXCEPTIONS

  • MASK_TOO_LONG

Read more ABAP Interview Questions and Answer


 Download attached file.

You must be Logged in to download this file

×