ABAP 二进制流 转换成本地文件 DEMO

2023-10-13 18:35:13 浏览数 (1)

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report YTEST0003
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YTEST0003.
DATA: FILENAME TYPE STRING VALUE 'C:a.txt'.

TYPES: BEGIN OF ITAB,
         FL1 TYPE STRING,
       END OF ITAB.

DATA: DATA_TAB TYPE STANDARD TABLE OF ITAB,
      WA_TAB   LIKE LINE OF DATA_TAB.

DATA : L_RC        TYPE  I,
       L_FILETABLE TYPE FILETABLE.
DATA:lv_string TYPE string.


PARAMETERS: P_FILE LIKE RLGRAP-FILENAME DEFAULT 'C:a.txt'.         "物料导入摸版

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = '请选择本地文件'
*     DEFAULT_EXTENSION       =
*     DEFAULT_FILENAME        =
      FILE_FILTER             = '文本文件 (*.txt)|*.txt|全部文件 (*.*)|*.*|'
      INITIAL_DIRECTORY       = SPACE
      "MULTISELECTION          = 'X'
    CHANGING
      FILE_TABLE              = L_FILETABLE
      RC                      = L_RC
*     USER_ACTION             =
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      OTHERS                  = 4.
  IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

START-OF-SELECTION.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
    EXPORTING
      FILENAME                = FILENAME
      FILETYPE                = 'ASC'
      CODEPAGE                = '8400'
    CHANGING
      DATA_TAB                = DATA_TAB
    EXCEPTIONS
      FILE_OPEN_ERROR         = 1
      FILE_READ_ERROR         = 2
      NO_BATCH                = 3
      GUI_REFUSE_FILETRANSFER = 4
      NO_AUTHORITY            = 6
      UNKNOWN_ERROR           = 7
      BAD_DATA_FORMAT         = 8
      UNKNOWN_DP_ERROR        = 12
      ACCESS_DENIED           = 13
      OTHERS                  = 17.

  LOOP AT DATA_TAB INTO WA_TAB.
    WRITE:/ WA_TAB-FL1.
  ENDLOOP.

  BREAK-POINT.

  DATA:XS TYPE XSTRING.
  DATA BIN_FILESIZE TYPE I.
  DATA TEMPTABLE    TYPE W3MIMETABTYPE.

  LOOP AT DATA_TAB INTO DATA(LS_TAB).


    CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
      EXPORTING
        INPUT         = LS_TAB-FL1
     IMPORTING
       OUTPUT        =  XS
              .


    BREAK-POINT.


*    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
*      EXPORTING
*        TEXT   =  LS_TAB-FL1
*        "MIMETYPE       = 'charset=gb2312'
*      IMPORTING
*        BUFFER = XS
*      EXCEPTIONS
*        FAILED = 1
*        OTHERS = 2.

    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        BUFFER        = XS
      IMPORTING
        OUTPUT_LENGTH = BIN_FILESIZE
      TABLES
        BINARY_TAB    = TEMPTABLE.


  BREAK-POINT.

    CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD(
    EXPORTING
    BIN_FILESIZE = BIN_FILESIZE
    FILENAME = 'D:a.xls'
    FILETYPE = 'BIN'
    CHANGING
    DATA_TAB = TEMPTABLE
).
  ENDLOOP.


  BREAK-POINT.

二进制流内容  BASE64转码过  使用钱先解码  然后直接  XSTRING 转binary   后续就可以直接操作对象了。

0 人点赞