Bootstrap

读取SMW0中txt文件,并解决乱码问题

1、文档说明 

根据需求,需要上传一段HTML代码,在功能中输出。所以,就需要将这段标准代码上传到SAP中用于读取展示。

2、需求实现

首先,准备HTML代码的txt文件

 运行事务码SMW0上传txt文件到SAP,选择二进制数据。

 上传附件对象

 系统中可能没有配置txt的文件格式,需要设置一下MIME类型

 选择菜单

 点击创建txt类型

通过函数WWWDATA_IMPORT读取二进制数据,之后通过函数SCMS_BINARY_TO_STRING将其转换为STRING字符串,在调试窗口的HTML视图下就能看到效果

 但有的系统会存在乱码问题,所以可以在函数SCMS_BINARY_TO_STRING前加入函数NLS_GET_FRONTEND_CP获取当前系统语言的编码,并将编码传到函数SCMS_BINARY_TO_STRING中

参考代码

REPORT ytest001.

DATA: lv_codepage_numc TYPE cpcodepage,
      lv_encoding      TYPE abap_encoding,
      lt_mime          TYPE TABLE OF w3mime,
      ls_mime          TYPE w3mime,
      lv_filesize      TYPE i,
      lv_xstring       TYPE xstring,
      ls_key           TYPE wwwdatatab.

DATA:lv_string TYPE string,
     lv_length TYPE i.

"查询SMW0文件
ls_key-relid = 'MI'.
ls_key-objid = 'ZHTML'.
SELECT * FROM wwwparams INTO TABLE @DATA(lt_wwwparam)
 WHERE relid = @ls_key-relid
   AND objid = @ls_key-objid.

READ TABLE lt_wwwparam INTO DATA(ls_wwwparam) WITH KEY name = 'filesize'.
IF sy-subrc EQ 0.
  lv_filesize = ls_wwwparam-value.
ENDIF.

* 获取SMW0的数据
CALL FUNCTION 'WWWDATA_IMPORT'
  EXPORTING
    key               = ls_key
  TABLES
    mime              = lt_mime
  EXCEPTIONS
    wrong_object_type = 1
    import_error      = 2
    OTHERS            = 99.

"获取当前语言编码
CALL FUNCTION 'NLS_GET_FRONTEND_CP'
  EXPORTING
    langu                 = sy-langu
    fetype                = 'MS'
  IMPORTING
    frontend_codepage     = lv_codepage_numc
  EXCEPTIONS
    illegal_syst_codepage = 1
    no_frontend_cp_found  = 2
    internal_or_db_error  = 3
    OTHERS                = 4.

IF sy-subrc <> 0.
  RETURN.
ENDIF.

lv_encoding = lv_codepage_numc."编码编号

"BIN转STRING
CALL FUNCTION 'SCMS_BINARY_TO_STRING'
  EXPORTING
    input_length  = lv_filesize
*   FIRST_LINE    = 0
*   LAST_LINE     = 0
*   MIMETYPE      = ' '
    encoding      = lv_encoding
  IMPORTING
    text_buffer   = lv_string
    output_length = lv_length
  TABLES
    binary_tab    = lt_mime
  EXCEPTIONS
    failed        = 1
    OTHERS        = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

WRITE:lv_string."输出的HTML语言文本

;