[ABAP] Datei-Upload

Variante 1 (cl_gui_frontend_services)

DATA: lv_rc TYPE i.
DATA: it_files TYPE filetable.
DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
TRY.
        cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter    = |pdf (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                              multiselection = abap_false
                                                    CHANGING  file_table     = it_files
                                                              rc             = lv_rc
                                                              user_action    = lv_action ).

    IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
      IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
        DATA(lv_filename) = it_files[ 1 ]-filename.

        DATA: lv_filesize TYPE i.
        DATA: it_bin_data TYPE STANDARD TABLE OF raw255.

* Bild auf Appl. Server hochladen (binary)
        cl_gui_frontend_services=>gui_upload( EXPORTING filename   = |{ lv_filename }|
														filetype   = 'BIN'
										      IMPORTING filelength = lv_filesize
										      CHANGING  data_tab   = it_bin_data ).														

        DATA: lv_xstr TYPE xstring.

* RAW (binary) nach xstring
        CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
          EXPORTING
            input_length = lv_filesize
          IMPORTING
            buffer       = lv_xstr
          TABLES
            binary_tab   = it_bin_data
          EXCEPTIONS
            failed       = 1
            OTHERS       = 2.

        ...
      ENDIF.
    ENDIF.

  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

Variante 2 (cl_secxml_helper)

TRY.
    cl_secxml_helper=>file_f4( EXPORTING initial_directory = ''
                                         window_title      = 'Dateiauswahl'
                               IMPORTING filename          = DATA(lv_xfile) ).


    IF lv_xfile IS NOT INITIAL.
      cl_secxml_helper=>upload_file( EXPORTING filename = lv_xfile
                                     IMPORTING bindata  = DATA(lv_data_xstr) ).
      
      ...
    ENDIF.

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Datei-Download

Variante 1 (cl_gui_frontend_services)

TRY.
* beliebiger Stream
    DATA: lv_xdata TYPE xstring.
    
    ...
    
* xstring -> solix
    DATA(it_solix_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xdata ).

* Daten lokal speichern
    cl_gui_frontend_services=>gui_download( EXPORTING
                                              filename     = |Bild.png|
                                              filetype     = 'BIN'
                                              bin_filesize = xstrlen( lv_xdata )
                                            CHANGING
                                              data_tab     = it_solix_data ).
CATCH cx_root INTO DATA(e_txt).
  WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (cl_gui_frontend_services)

* Inhalt einer internen Tabelle als Datei (CSV) auf den lokalen Rechner speichern:
* http://help.sap.com/saphelp_nw70ehp2/helpdata/de/c7/5ab8ec178c44a8aacd1dcac3460db8/content.htm  

* CSV-Daten
DATA: itab TYPE STANDARD TABLE OF string.
* Dateipfad + Dateiname
DATA: lv_file TYPE string VALUE 'c:\temp\test.txt'.

...

TRY.
  cl_gui_frontend_services=>gui_download( EXPORTING
                                            filename = lv_file
                                            filetype = 'DAT'  " mögliche Filetypen: BIN, ASC, DAT, DBF, WK1, VSS, IBM
                                            codepage = '4110' " UTF-8 Encoding: 4110, UTF-16 Big Endian : 4102, UTF-16 Little Endian : 4103.
                                          CHANGING
                                            data_tab = itab ).
      
    WRITE: / |Datei erfolgreich unter { lv_file } gespeichert.|.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

Variante 3 (cl_secxml_helper)

TRY.
* Daten als Stream
    DATA: lv_xdata TYPE xstring.

    ...

* File lokal speichern auf dem Frontend
    cl_secxml_helper=>save_file( EXPORTING default_file_name = ''
                                           initial_directory = ''
                                           window_title      = 'Datei speichern'
                                           bindata           = lv_xdata ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] OpenFile-Dialog anzeigen

Variante 1 (cl_gui_frontend_services)

PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
  
  DATA: lv_rc TYPE i.
  DATA: it_files TYPE filetable.
  DATA: lv_action TYPE i.
    
* File-Tabelle leeren, da hier noch alte Einträge von vorherigen Aufrufen drin stehen können  
  CLEAR it_files.

* FileOpen-Dialog aufrufen
  TRY.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                    file_filter    = |txt (*.txt)\|*.txt\|{ cl_gui_frontend_services=>filetype_all }|
                                                    multiselection = abap_false
                                                  CHANGING
                                                    file_table  = it_files
                                                    rc          = lv_rc
                                                    user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          p_fname = it_files[ 1 ]-filename.
        ENDIF.
      ENDIF.
      
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'I'.
  ENDTRY.

Variante 2 (cl_openxml_helper)

DATA(lv_default_filename) = ||.
DATA(lv_filterpattern) = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|.

DATA(lv_selected_file) = cl_openxml_helper=>browse_local_file_open( iv_title      = 'Dateiauswahl'
                                                                    iv_filename   = lv_default_filename
                                                                    iv_extpattern = lv_filterpattern ).

WRITE: / lv_selected_file.

Variante 3 (cl_rsan_ut_files)

* F4-Auswahldialog (PopUp) für Auswahl einer Datei auf dem Applikationsserver
PARAMETERS : p_filenm LIKE ibipparms-path OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_filenm.

  DATA: lv_canceled TYPE boolean.
  DATA: lv_file_name TYPE string.

  TRY.
      cl_rsan_ut_files=>f4( EXPORTING
                              i_applserv         = abap_true " Dateiauswahl vom Appl.-Server holen, sonst GUI
                              i_title            = 'Dateiauswahl auf dem Applikationsserver'
                              i_gui_extension    = ''
                              i_gui_ext_filter   = ''
                              i_applserv_logical = abap_false
                              i_applserv_al11    = abap_true
                            IMPORTING
                              e_canceled         = lv_canceled
                            CHANGING
                              c_file_name        = lv_file_name ).

      IF lv_canceled NE abap_true.
        p_filenm = lv_file_name.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'I'.
  ENDTRY.

Variante 4 (cl_secxml_helper)

TRY.
    cl_secxml_helper=>file_f4( EXPORTING initial_directory = ''
                                         window_title      = 'Dateiauswahl'
                               IMPORTING filename          = DATA(lv_xfile) ).


  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 5 (F4_DXFILENAME_4_DYNP)

* Flexibles Dateiauswahl-Popup für die F4-Suchhilfe
PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

* F4-Hilfe für Dateien; Aufruf für Dynpro
  CALL FUNCTION 'F4_DXFILENAME_4_DYNP'
    EXPORTING
      dynpfield_filename = 'P_FILE' " Dynprofeld für Rückgabewert
      dyname             = sy-repid
      dynumb             = sy-dynnr
      filetype           = 'P'  " P -> Physical, L -> Logical
      location           = 'A'  " A -> Application Server, P -> Presentation Server
      server             = '?'. " leer -> Default-Anmeldeserver, ? -> Auswahl-Popup für Serverauswahl

Variante 6 (F4_DXFILENAME_TOPRECURSION)

* Auswahldialog für Dateien auf dem Applikationsserver
* siehe auch: cl_rsan_ut_files=>f4( ).
DATA: lv_location_flag TYPE dxfields-location.
DATA: lv_server TYPE msxxlist-name.
DATA: lv_path TYPE dxfields-longpath.
DATA: lv_abend_flag TYPE dxfields-abendflag.

* F4-Hilfe für Dateien
CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
  EXPORTING
    i_location_flag       = 'A' " A - Applikationsserver, P - Presentationsserver
    i_server              = '?' " ? - Abfragedialog für Servername anzeigen, ' ' - akt. Anmeldeserver, sonst Servernamen eintragen
    i_path                = '/usr/sap' " Startpfad
  IMPORTING
    o_location_flag       = lv_location_flag
    o_server              = lv_server
    o_path                = lv_path
    abend_flag            = lv_abend_flag
  EXCEPTIONS
    communication_failure = 1
    system_failure        = 2
    rfc_error             = 3.

IF sy-subrc = 0.

  WRITE: / 'Location flag:', lv_location_flag.
  WRITE: / '       Server:', lv_server.
  WRITE: / ' Pfad + Datei:', lv_path.
  WRITE: / '   Abend Flag:', lv_abend_flag.

ENDIF.

Variante 7 (F4_FILENAME – Obsolet)

* Eingabefeld für Dateinamen auf dem Selektionsbild, Eingabe ist Pflicht
PARAMETERS : p_file LIKE ibipparms-path OBLIGATORY.

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CLEAR p_file.

* F4 for filename / Filemanager support to locate file in a directory
  CALL FUNCTION 'F4_FILENAME' " File-Dialog aufrufen
    EXPORTING
      field_name = 'P_FILE'
    IMPORTING
      file_name  = p_file.