Menu

Read a File into a Variable in ABAP

DevWorkbench    Saturday August 23rd, 2014   

Example report to upload files from the desktop into an ABAP variable

REPORT zdemofileupload.
 
PARAMETERS p_fname TYPE char128 OBLIGATORY.
 
DATA gt_filetable TYPE filetable.
DATA gs_filetable TYPE file_table.
DATA gv_return    TYPE i.
 
DATA gv_filename TYPE string.
DATA gt_line     TYPE TABLE OF string.
DATA gv_data     TYPE string.
 
* When the user presses F4, show the file open dialog
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
 
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    CHANGING
      file_table              gt_filetable
      rc                      gv_return
    EXCEPTIONS
      file_open_dialog_failed 1
      cntl_error              2
      error_no_gui            3
      not_supported_by_gui    4
      OTHERS                  5.
 
* Read the file name table and move it to the param
  READ TABLE gt_filetable INTO gs_filetable INDEX 1.
  p_fname gs_filetable.
 
START-OF-SELECTION.
* The gui_upload method wants the filename as string
  gv_filename p_fname.
 
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                gv_filename
    CHANGING
      data_tab                gt_line
    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.