Vorlage Upload/Download Datei Frontend

Aus SAP-Wiki
Wechseln zu: Navigation, Suche
*&---------------------------------------------------------------------*
*& Report  ZBC_UPLOAD_DOWNLOAD
*&
*&---------------------------------------------------------------------*
*& Eberstein Consulting
*& René Eberstein
*& Paket =
*& Upload vom Frontend und Download auf Frontend
*&---------------------------------------------------------------------*
*   Ä N D E R U N G E N
*&---------------------------------------------------------------------*
 
REPORT ZRD_UPLOAD_DOWNLOAD.
****************
* DATA & TYPES *
****************
TYPES: BEGIN OF Ty_FILE,
       DATA(900),
       END OF Ty_FILE.
Types: tty_file type STANDARD TABLE OF ty_file.

DATA:  GT_FILE TYPE STANDARD TABLE OF Ty_FILE,
       GT_UPLOAD type STANDARD TABLE OF ZRD_SPEC_UPLOAD,
       gt_outfile type tty_file.

Data: gv_down_file TYPE RLGRAP-FILENAME.
Types: begin of ty_itab,
            Text type text100,
           End of ty_itab.

Types: tty_itab type STANDARD TABLE OF Ty_itab.
**********************
* PARAMETER - SCREEN *
**********************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. "Selektion

Parameters: P_FILE   TYPE RLGRAP-FILENAME,
            p_split  type char1           default '|'.
SELECTION-SCREEN END OF BLOCK b1.
****************************************
* AT SELECTION-SCREEN ON VALUE-REQUEST *
****************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
* Open-File-Dialog zur Auswahl Inputdatei  
  PERFORM GET_FILENAME.
***********************
* AT SELECTION-SCREEN *
***********************
at selection-screen.
* Die Download-Datei wird im gleichen Verzeichnis gespeichert
* Nur mit zusätzlichen Namen "_conv" und als TXT  
  perform get_down_file
    using p_file
    changing gv_down_file.
**********************
* START-OF-SELECTION *
**********************
start-of-selection.

* Datei hochladen in Flatfile von Frontend
  perform upload_file
    using P_FILE
    changing gt_file.

* Flatfile in strukturierte interne Tabelle GT_UPLOAD konvertieren
  perform read_flatfile_into_itab
    using    gt_file
    changing gt_itab.

* Fülle Ausgabestruktur aus Uploaddatei und Headerdaten
  perform fill_output
    using    gt_upload
    changing gt_outfile.

* Download konvertierte Datei auf Frontend
  perform download
    using gv_down_file
          gt_outfile.

  message i531(0u) with 'Upload und Download komplett'(002).
*&---------------------------------------------------------------------*
*&      Form  GET_FILENAME
*&---------------------------------------------------------------------*
*       Popupdialog für Uploadpfad in Parameter laden
*----------------------------------------------------------------------*
FORM GET_FILENAME.

  DATA: lt_FILE_TABLE TYPE FILETABLE,
        ls_FILE       TYPE FILE_TABLE,
        lv_RC         TYPE I.

  LS_FILE-FILENAME = P_FILE.
  APPEND LS_FILE TO LT_FILE_TABLE.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
*    EXPORTING
*      WINDOW_TITLE            =
*      DEFAULT_EXTENSION       =
*      DEFAULT_FILENAME        =
*      FILE_FILTER             =
*      WITH_ENCODING           =
*      INITIAL_DIRECTORY       =
*      MULTISELECTION          =
    CHANGING
      FILE_TABLE              = LT_FILE_TABLE
      RC                      = LV_RC
*     USER_ACTION             =
*     FILE_ENCODING           =
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      NOT_SUPPORTED_BY_GUI    = 4
      OTHERS                  = 5.

  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  READ TABLE LT_FILE_TABLE INTO LS_FILE INDEX 1.

  IF SY-SUBRC = 0.
    P_FILE = LS_FILE-FILENAME.
  ENDIF.

ENDFORM.                    " GET_FILENAME
*&---------------------------------------------------------------------*
*&      Form  UPLOAD_FILE
*&---------------------------------------------------------------------*
*       Upload Datei
*----------------------------------------------------------------------*
FORM UPLOAD_FILE
   using piv_FILE   TYPE RLGRAP-FILENAME
   changing pct_file type tty_file.

  Data: lv_filename type string.

  lv_filename = piv_file.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
    EXPORTING
       FILENAME                = lv_filename
*      FILETYPE                = 'ASC'
*      HAS_FIELD_SEPARATOR     = SPACE
*      HEADER_LENGTH           = 0
*      READ_BY_LINE            = 'X'
*      DAT_MODE                = SPACE
*      CODEPAGE                = SPACE
*      IGNORE_CERR             = ABAP_TRUE
*      REPLACEMENT             = '#'
*      VIRUS_SCAN_PROFILE      =
*    IMPORTING
*      FILELENGTH              =
*      HEADER                  =
    CHANGING
      DATA_TAB                = pct_FILE
    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.

  if sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  endif.

ENDFORM.                    " UPLOAD_FILE
*&---------------------------------------------------------------------*
*&      Form  READ_FLATFILE_INTO_ITAB
*&---------------------------------------------------------------------*
*       Flatfile in strukturierte ITAB konvertieren
*       Struktur = ZRD_SPEC_UPLOAD
*----------------------------------------------------------------------*
FORM READ_FLATFILE_INTO_ITAB  USING    IT_FILE    type tty_file
                              CHANGING CT_UPLOAD type tty_upload.

  Data: ls_file   type ty_file,
        ls_itab type ty_itab.

  loop at it_file into ls_file.

    clear ls_itab.

    split ls_file-data
          at p_split
          into ... "Felder Struktur aufführen

    append ls_itab to ct_upload.

  endloop.

ENDFORM.                    " READ_FLATFILE_INTO_ITAB
*&---------------------------------------------------------------------*
*&      Form  FILL_OUTPUT
*&---------------------------------------------------------------------*
*       Füllen Outfile
*----------------------------------------------------------------------*
FORM FILL_OUTPUT  USING    IT_UPLOAD  type tty_upload
                  CHANGING CT_OUTFILE type tty_file.

  Data: ls_file   type ty_file,
        ls_upload type Ty_itab.

*********************
* Füllen Positionen *
*********************
  loop at IT_itab into ls_upload.

    check sy-tabix <> 1. "Überschriftenzeile wird nicht verarbeitet


    "Felder CT_OUTFILE füllen

  endloop.

ENDFORM.                    " FILL_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  GET_DOWN_FILE
*&---------------------------------------------------------------------*
*       Fülle Pfad für Downloadfile
*----------------------------------------------------------------------*
FORM GET_DOWN_FILE  USING    iv_FILE      type RLGRAP-FILENAME
                    CHANGING cv_DOWN_FILE TYPE RLGRAP-FILENAME.

  Data: lv_length type i.

  lv_length = strlen( piv_file ).

* Inputfilepfad kürzen um ".csv"
  lv_length = lv_length - 4.

* Inputfilepfad lediglich ergänzen um "_conv_txt"
  concatenate piv_file(lv_length)
              '_conv.txt'
              into pcv_down_file.

ENDFORM.                    " GET_DOWN_FILE
*&---------------------------------------------------------------------*
*&      Form  DOWNLOAD
*&---------------------------------------------------------------------*
*       Download konvertiertes File
*----------------------------------------------------------------------*
FORM DOWNLOAD  USING    IV_DOWN_FILE type RLGRAP-FILENAME
                        IT_OUTFILE   type tty_file.

  Data: lv_filename type string.

  lv_filename = piv_down_file.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
*      BIN_FILESIZE              =
     FILENAME                  = lv_filename
*      FILETYPE                  = 'ASC'
*      APPEND                    = SPACE
*      WRITE_FIELD_SEPARATOR     = SPACE
*      HEADER                    = '00'
*      TRUNC_TRAILING_BLANKS     = SPACE
*      WRITE_LF                  = 'X'
*      COL_SELECT                = SPACE
*      COL_SELECT_MASK           = SPACE
*      DAT_MODE                  = SPACE
*      CONFIRM_OVERWRITE         = SPACE
*      NO_AUTH_CHECK             = SPACE
*      CODEPAGE                  = SPACE
*      IGNORE_CERR               = ABAP_TRUE
*      REPLACEMENT               = '#'
*      WRITE_BOM                 = SPACE
*      TRUNC_TRAILING_BLANKS_EOL = 'X'
*      WK1_N_FORMAT              = SPACE
*      WK1_N_SIZE                = SPACE
*      WK1_T_FORMAT              = SPACE
*      WK1_T_SIZE                = SPACE
*    IMPORTING
*      FILELENGTH                =
    CHANGING
      DATA_TAB                  = it_outfile
    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
      NOT_SUPPORTED_BY_GUI      = 22
      ERROR_NO_GUI              = 23
      OTHERS                    = 24.

  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    MESSAGE I800(29) WITH piv_down_file.
    MESSAGE I821(29).                                       "note509559
    LEAVE PROGRAM.                                          "note509559
  ENDIF.

ENDFORM.                    " DOWNLOAD