Upload file to application server (AL11) - aidamate13/AMS-Knowledge-Base GitHub Wiki

Use GUI_UPLOAD to Read the File from the Local PC: The GUI_UPLOAD function module is used to read the file from the local machine into an internal table.

Use OPEN DATASET to Open/Create the File on the Application Server: The OPEN DATASET command is used to open the target file on the application server.

Use TRANSFER to Write Data from the Internal Table to the Application Server: The TRANSFER command is used to write the data from the internal table to the file on the application server.


DATA: lv_file_name   TYPE string,
      lv_file_path   TYPE string,
      lv_line        TYPE string,
      lt_file_data   TYPE TABLE OF string,
      lv_fullpath    TYPE string.

" Local file path (on your PC)
lv_file_name = 'C:\temp\YOUR_LOCAL_FILENAME.TXT'.

" File path on the application server (e.g., from AL11)
lv_file_path = '/usr/sap/trans/data/YOUR_SERVER_FILENAME.TXT'.

" Upload the local file into an internal table
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename   = lv_file_name
    filetype   = 'ASC'
  TABLES
    data_tab   = lt_file_data
  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
    file_not_found         = 17
    dataprovider_exception = 18
    control_flush_error    = 19
    others                 = 20.

IF sy-subrc <> 0.
  WRITE: / 'Error uploading file from local machine'.
  EXIT.
ENDIF.

" Open the dataset on the application server for writing
OPEN DATASET lv_file_path FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc = 0.
  " Write each line from the internal table to the dataset
  LOOP AT lt_file_data INTO lv_line.
    TRANSFER lv_line TO lv_file_path.
  ENDLOOP.

  " Close the dataset after writing
  CLOSE DATASET lv_file_path.
ELSE.
  " Handle the error if the file cannot be opened/created
  WRITE: / 'Error opening file on application server'.
ENDIF.