Download File to SAP Presentation 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.
  2. Fill the internal table with required data.
  3. Use function module GUI_DOWNLOAD’ or ‘GUI_DOWNLOAD’ method of ‘CL_GUI_FRONTEND_SERVICES’ class to download the data.

Below program uses function module GUI_DOWNLOAD to download the file.

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

DATA: gv_filename TYPE string,
      gv_filetype TYPE char10.
*----------------------------------------------------------------------*
*     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.
*Move complete file path to file name
  gv_filename = 'C:\test\data.txt'.

*Download the internal table data into a file in SAP presentation server
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = gv_filename
      filetype                = 'ASC'
      write_field_separator   = 'X'
    TABLES
      data_tab                = gt_spfli.

ENDFORM.                    " save_file

When you execute the above program, the file ‘data.txt’ will be downloaded to ‘C:\test’.

download-file-presentation-server

We can also use GUI_DOWNLOAD method of CL_GUI_FRONTEND_SERVICES class to download the file. Instead of calling GUI_DOWNLOAD function module in the save_file subroutine of above program, call the GUI_DOWNLOAD method of CL_GUI_FRONTEND_SERVICES class.

CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = gv_filename
      filetype              = 'ASC'
      write_field_separator = 'X'
    CHANGING
      data_tab              = gt_spfli.