Download File to SAP Application Server using ABAP

Use the following steps to download the ABAP internal table data to a file in SAP application server.

  1. Declare a ABAP internal table and fill the internal table with required data.
  2. Use OPEN DATASET ABAP statement to open/create a file on the SAP application server.
  3. Loop through the internal table and use TRANSFER ABAP statement to move each internal table record to file on application server.
  4. Close the file on the application server using CLOSE DATASET ABAP statement.

Below program uses OPEN DATASET, TRANSFER and CLOSE DATASET statements to download the file.

*----------------------------------------------------------------------*
*     Data Decalaration
*----------------------------------------------------------------------*
DATA: gt_spfli  TYPE TABLE OF spfli,
      gwa_spfli TYPE spfli.
DATA: gv_file   TYPE rlgrap-filename.

*----------------------------------------------------------------------*
*     START-OF-SELECTION
*----------------------------------------------------------------------*
PERFORM get_data.
IF NOT gt_spfli[] IS INITIAL.
  PERFORM save_file.
ELSE.
  MESSAGE 'No data found' TYPE 'I'.
ENDIF.
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
FORM get_data.
*Get data from table SPFLI
  SELECT * FROM spfli
         INTO TABLE gt_spfli.
ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  save_file
*&---------------------------------------------------------------------*
FORM save_file.
  DATA: lv_data TYPE string.

*Move complete path to filename
  gv_file = 'spfli.txt'.

* Open the file in output mode
  OPEN DATASET gv_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc NE 0.
    MESSAGE 'Unable to create file' TYPE 'I'.
    EXIT.
  ENDIF.

  LOOP AT gt_spfli INTO gwa_spfli.
    CONCATENATE gwa_spfli-carrid
                gwa_spfli-connid
                gwa_spfli-countryfr
                gwa_spfli-cityfrom
                gwa_spfli-airpfrom
                gwa_spfli-countryto
                gwa_spfli-cityto
                gwa_spfli-airpto
                gwa_spfli-arrtime
     INTO lv_data
     SEPARATED BY ','.
*TRANSFER moves the above fields from workarea to file  with comma
*delimited format
    TRANSFER lv_data TO gv_file.
    CLEAR: gwa_spfli.
  ENDLOOP.
* close the file
  CLOSE DATASET gv_file.

ENDFORM.                    " save_file

When you execute the above program, the data in the internal table will be downloaded to a file on the application server. Use t-code AL11 to view the file on SAP application server.

sap-download-application-server

Just double click on the file “spfli.txt” to view the contents.

sap-download-application-server-2