Menu

Create and Download a File from ABAP

2014-08-24       

After yesterday’s post about uploading files into ABAP variables, let’s look at things the other way round – how to provide data for download by your users.

I don’t think it comes as a huge surprise to you that CL_GUI_FRONTEND_SERVICES is, once again, the tool of our choice to do the task. The way to do it is quite similar to yesterday’s post about file uploading. Instead of the methods FILE_OPEN_DIALOG and GUI_UPLOAD, we will make use of FILE_SAVE_DIALOG and GUI_DOWNLOAD.

Let’s look at the code. We need some data first, so I’m putting some dummy data into a table that we can export later. The next step is to call the method that displays a file save dialog and get the name and path under which the file should be saved.

* Add some data to the table
gv_data = 'All your base'.
APPEND gv_data TO gt_data.
gv_data = 'are belong to us'.
APPEND gv_data TO gt_data.
 
* Show the file download dialog
CALL METHOD cl_gui_frontend_services=>file_save_dialog
  EXPORTING
    default_file_name    = 'file.txt'
    default_extension    = 'TXT'
  CHANGING
    filename             = gv_filename
    path                 = gv_path
    fullpath             = gv_fullpath
    user_action          = gv_useraction
  EXCEPTIONS
    cntl_error           = 1
    error_no_gui         = 2
    not_supported_by_gui = 3
    OTHERS               = 4.

After checking if the user really clicked OK, we can move on to performing the actual file download. All we need to provide is a file name and the sample data as a string table.

* Check if the user clicked OK
IF gv_useraction <> 0.
  EXIT.
ENDIF.
 
* Do the actual download
CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    filename = gv_filename
  CHANGING
    data_tab = gt_data
  EXCEPTIONS
    OTHERS   = 24.

 

…aaaand we’re done! Even simpler than file uploading, isn’t it? This makes it easy for your users to import and export data to and from SAP. Of course, there’s a lot more you can do – just check out all the method parameters of both methods. Enjoy playing around!

Source Code: Download Files in ABAP

REPORT zdemofiledownload.
 
DATA gv_filename TYPE string.
DATA gv_path TYPE string.
DATA gv_fullpath TYPE string.
DATA gv_useraction TYPE i.
DATA gt_data TYPE TABLE OF string.
DATA gv_data TYPE string.
 
* Add some data to the table
gv_data = 'All your base'.
APPEND gv_data TO gt_data.
gv_data = 'are belong to us'.
APPEND gv_data TO gt_data.
 
* Show the file download dialog
CALL METHOD cl_gui_frontend_services=>file_save_dialog
  EXPORTING
    default_file_name    = 'file.txt'
    default_extension    = 'TXT'
  CHANGING
    filename             = gv_filename
    path                 = gv_path
    fullpath             = gv_fullpath
    user_action          = gv_useraction
  EXCEPTIONS
    cntl_error           = 1
    error_no_gui         = 2
    not_supported_by_gui = 3
    OTHERS               = 4.
 
* Check if the user clicked OK
IF gv_useraction <> 0.
  EXIT.
ENDIF.
 
* Do the actual download
CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    filename = gv_filename
  CHANGING
    data_tab = gt_data
  EXCEPTIONS
    OTHERS   = 24.