SAP ABAP Program Downloading Files from Application Server to Local System

SAP ABAP Program: Downloading Files from Application Server to Local System.

SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System

In This Article we will see how to Download Files from Application Server to Local System by using SAP ABAP Program.

SAP ABAP Program: Downloading Files from Application Server to Local System

In SAP ABAP, there are so many times when user or functional need to transfer files between the application server and the local system.

This could be to back up important data, generate reports, or share files with external systems. In this article we will explore how to create an ABAP program that allows us to download files from the application server to the local system effortlessly.

SAP ABAP Program Downloading files from application server to local system we have to follow the below steps.

STEP 1: Files Selection(SAP ABAP Program Downloading Files from Application Server to Local System)

This First step is to enable users to select the file they wish to download from the application server. We can user the standard SAP Function Module “/SAPDMC/LSM_F4_SERVER_FILE” which is use to create file selection dialog from the application server, allowing users to choose the desired file. If you want to choose file from your local system then you can user Function Module “F4_FILENAME” to create a file selection dialog. This function module opens a file dialog box, allowing users to choose the desired file.

/SAPDMC/LSM_F4_SERVER_FILE
F4_FILENAME
/SAPDMC/LSM_F4_SERVER_FILE

STEP 2: Read File Content(SAP ABAP Program Downloading Files from Application Server to Local System)

Once the user select the file, the next step to read its content from the application server. We can do this by using “OPEN DATASE” then “READ DATASET” and then finally “CLOSE DATASET“.

OPEN DATASET READ DATASET CLOSE DATASET
READ DATASET

STEP 3: Download the File(SAP ABAP Program Downloading Files from Application Server to Local System)

After reading the file content into a internal table, we can then download the file to the local system using the “GUI_DOWNLOAD” function module. This modules saves the file to the local system with the specified file path and name.

image 7

*&———————————————————————*
*& Report  ZDOWNLOAD_APPSERVER
*&
*&———————————————————————*
*&
*&
*&———————————————————————*
REPORT ZDOWNLOAD_APPSERVER.

types: begin of ty_text,
  text(1000) type c,
  end of ty_text.

  data: it_text type table of ty_text,
        wa_text type          ty_text,
        wa_string type string.

  constants: lv_sloc type string value ‘C:\GST\GSTR2\’.

  data: gv_result type c,
        gv_rc type i.

  selection-screen: begin of block b1 with frame title text-001.
    parameters: p_loc type rlgrap-filename obligatory.
    selection-screen: end of block b1.

 At selection-screen on value-request for p_loc.
   CALL FUNCTION ‘/SAPDMC/LSM_F4_SERVER_FILE’
    EXPORTING
      DIRECTORY              = p_loc
    IMPORTING
      SERVERFILE             = p_loc
    EXCEPTIONS
      CANCELED_BY_USER       = 1
      OTHERS                 = 2
             .
   IF sy-subrc <> 0.
* Implement suitable error handling here
   ENDIF.


    START-OF-SELECTION.


    open dataset p_loc for input in text mode encoding default.
    if sy-subrc ne 0.
      message ‘file not found’ type ‘E’.
      else.
        do.
          read dataset p_loc into wa_string.
          if sy-subrc ne 0.
            exit.
            else.
              if wa_string is not initial.
                wa_text-text = wa_string.
                append wa_text to it_text.
                clear wa_text.
              endif.
          endif.
        enddo.
    endif.
    BREAK-POINT.


      CLEAR : gv_result, gv_rc.
  CALL METHOD cl_gui_frontend_services=>directory_exist
    EXPORTING
      directory            = lv_sloc
    RECEIVING
      result               = gv_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF sy-subrc = 0.

    IF gv_result <> ‘X’.

      CALL METHOD cl_gui_frontend_services=>directory_create
        EXPORTING
          directory                = lv_sloc
        CHANGING
          rc                       = gv_rc
        EXCEPTIONS
          directory_create_failed  = 1
          cntl_error               = 2
          error_no_gui             = 3
          directory_access_denied  = 4
          directory_already_exists = 5
          path_not_found           = 6
          unknown_error            = 7
          not_supported_by_gui     = 8
          wrong_parameter          = 9
          OTHERS                   = 10.
      IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    ENDIF.

  ENDIF.

  data(lv_filename) = |{ lv_sloc }| && |test.txt|.


  CALL FUNCTION ‘GUI_DOWNLOAD’
    EXPORTING
*     BIN_FILESIZE                    =
      filename                        = lv_filename
     FILETYPE                        = ‘DAT’
*     APPEND                          = ‘ ‘
*     WRITE_FIELD_SEPARATOR           = ‘ ‘
*     HEADER                          = ’00’
*     TRUNC_TRAILING_BLANKS           = ‘ ‘
*     WRITE_LF                        = ‘X’
*     COL_SELECT                      = ‘ ‘
*     COL_SELECT_MASK                 = ‘ ‘
*     DAT_MODE                        = ‘ ‘
*     CONFIRM_OVERWRITE               = ‘ ‘
*     NO_AUTH_CHECK                   = ‘ ‘
*     CODEPAGE                        = ‘ ‘
*     IGNORE_CERR                     = ABAP_TRUE
*     REPLACEMENT                     = ‘#’
*     WRITE_BOM                       = ‘ ‘
*     TRUNC_TRAILING_BLANKS_EOL       = ‘X’
*     WK1_N_FORMAT                    = ‘ ‘
*     WK1_N_SIZE                      = ‘ ‘
*     WK1_T_FORMAT                    = ‘ ‘
*     WK1_T_SIZE                      = ‘ ‘
*     WRITE_LF_AFTER_LAST_LINE        = ABAP_TRUE
*     SHOW_TRANSFER_STATUS            = ABAP_TRUE
*     VIRUS_SCAN_PROFILE              = ‘/SCET/GUI_DOWNLOAD’
*   IMPORTING
*     FILELENGTH                      =
    tables
      data_tab                        = IT_TEXT
*     FIELDNAMES                      =
   EXCEPTIONS
     FILE_WRITE_ERROR                = 1
     NO_BATCH                        = 2
     GUI_REFUSE_FILETRANSFER         = 3
     INVALID_TYPE                    = 4
     NO_AUTHORITY                    = 5
     UNKNOWN_ERROR                   = 6
     HEADER_NOT_ALLOWED              = 7
     SEPARATOR_NOT_ALLOWED           = 8
     FILESIZE_NOT_ALLOWED            = 9
     HEADER_TOO_LONG                 = 10
     DP_ERROR_CREATE                 = 11
     DP_ERROR_SEND                   = 12
     DP_ERROR_WRITE                  = 13
     UNKNOWN_DP_ERROR                = 14
     ACCESS_DENIED                   = 15
     DP_OUT_OF_MEMORY                = 16
     DISK_FULL                       = 17
     DP_TIMEOUT                      = 18
     FILE_NOT_FOUND                  = 19
     DATAPROVIDER_EXCEPTION          = 20
     CONTROL_FLUSH_ERROR             = 21
     OTHERS                          = 22
            .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System
SAP ABAP Program Downloading Files from Application Server to Local System

You May Also Like:

For the next blog please connect with us and follow us on twitter.com/einfonett

Leave a Comment