在 ABAP 中将字符串响应转换为 xstring 会导致文件损坏

问题描述 投票:0回答:0

我正在尝试根据更改请求将文件附加到 SAP MDG。我收到从 Ariba API 查询的文件附件的字符串响应,如下所示:。附件是一个“XLSX”文件。

我将文件附加到 CR 的代码是这样的:

      DATA: lv_xresponse TYPE xstring,
            lv_xrlen     TYPE i,
            lv_tsl       TYPE timestampl,
            lv_ftype     TYPE c LENGTH 100,
            lv_filetype  TYPE c LENGTH 100,
            ls_file      TYPE usmd_s_attachment_data,

      CALL FUNCTION 'SCMS_STRING_TO_XSTRING' "To convert string to xtring
        EXPORTING
          text     = lv_response 
          mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        IMPORTING
          buffer   = lv_xresponse. " 
     CONCATENATE cl_abap_char_utilities=>byte_order_mark_little 
      lv_xresponse INTO lv_xresponse IN BYTE MODE.

      lv_xrlen = xstrlen( lv_xresponse ).
      DATA(lo_cr_api) = cl_usmd_gov_api=>get_instance( iv_model_name = 'BP' ).
      lo_cr_api->enqueue_crequest( iv_crequest_id = lv_crequest ).

      ls_file-usmd_title = lv_filename.
      ls_file-usmd_file_type = lv_filetype.
      ls_file-usmd_content = lv_xresponse. "response converted to XSTRING
      ls_file-usmd_file_size = lv_xrlen.

      CALL METHOD lo_cr_api->add_attachment
        EXPORTING
          iv_crequest_id = lv_crequest
          is_attachment  = ls_file.
      IF sy-subrc = 0.
        CALL METHOD lo_cr_api->save.
      ENDIF.

当我从CR查看附件并下载它时,这是Excel的错误:

我认为问题在于将字符串响应转换为

xstring
。我可以直接从 Postman 下载文件并保存为“xlsx”,但是从 ABAP,我需要将其转换为
xstring 
因为它似乎只接受 xstring 作为内容,在这部分
ls_file-usmd_content = lv_xresponse
。我尝试了功能模块
SCMS_STRING_TO_XSTRING
,但这似乎不起作用。关于如何将文件(带有字符串响应)正确转换为
xstring
的任何建议?

更新:我尝试将

base64
解码为
xstring
使用:
cl_http_utility=>if_http_utility~decode_x_base64
但结果仍然相同。我注意到与原始尺寸相比,转换为
xstring
时尺寸减小了。

abap sap
© www.soinside.com 2019 - 2024. All rights reserved.