关于abap:ABAP-REST-API-FORMDATA

115次阅读

共计 6806 个字符,预计需要花费 18 分钟才能阅读完成。

FUNCTION zhr_intf_per_pho2cms.
*"----------------------------------------------------------------------
*"*" 本地接口:*"  IMPORTING
*"     REFERENCE(IV_PERNR) TYPE  PERNR_D OPTIONAL
*"     REFERENCE(IV_GUID) TYPE  ZE_EHR_GUID OPTIONAL
*"  EXPORTING
*"     REFERENCE(EV_PHOTO) TYPE  STRING
*"     REFERENCE(ES_MESSAGE) TYPE  ZSEHR_MESSAGE
*"----------------------------------------------------------------------
  DATA:lv_objid(50).
  DATA: lt_toav0   TYPE toav0   OCCURS 1 WITH HEADER LINE.
  DATA: lt_bindata LIKE tbl1024 OCCURS 0 WITH HEADER LINE.
  DATA:lt_bin TYPE TABLE OF sdokcntbin.
  DATA:lt_info TYPE TABLE OF scms_acinf,
       ls_info TYPE scms_acinf.
  DATA:l_str TYPE string.
  DATA: lv_bindata TYPE xstring.
  DATA: lv_count   TYPE sy-index.
  DATA: lv_length  TYPE sapb-length.
  DATA: ls_toahr     TYPE toahr,
        lv_object_id TYPE toahr-object_id.
  DATA:host TYPE string.
  DATA:client TYPE REF TO if_http_client.
  DATA: it_formulario TYPE tihttpnvp,
        lv_error_msg  TYPE string,
        wa_formulario LIKE LINE OF it_formulario,
        len           TYPE i,
        part          TYPE REF TO if_http_entity.
  CLEAR ev_photo.
*  CONCATENATE IV_PERNR '0002' INTO LV_OBJID.
  IF iv_pernr IS NOT INITIAL.
    lv_objid = iv_pernr.
    lv_object_id = iv_pernr .                               "&&'0002'.
    SELECT SINGLE * INTO  ls_toahr FROM toahr WHERE sap_object = 'PREL' AND (object_id = lv_objid OR object_id = lv_object_id) AND archiv_id = 'A2'.
    IF sy-subrc NE 0.
      SELECT SINGLE guid INTO lv_objid FROM zthr0099 WHERE  ztype IN ('ZZ01','Z001') AND objid = iv_pernr AND zstat NOT IN ('D', '7', '3').
    ELSE.
      CLEAR lv_objid.
      CONDENSE:ls_toahr-object_id NO-GAPS.
      lv_objid = ls_toahr-object_id.
    ENDIF.
  ELSE.
    lv_objid = iv_guid.
  ENDIF.

  REFRESH lt_toav0.
  CALL FUNCTION 'ARCHIV_GET_CONNECTIONS'
    EXPORTING
      objecttype    = 'PREL'
      object_id     = lv_objid
      client        = sy-mandt
      archiv_id     = 'A2'
      documenttype  = 'HRICOLFOTO'
    IMPORTING
      count         = lv_count
    TABLES
      connections   = lt_toav0
    EXCEPTIONS
      nothing_found = 1
      OTHERS        = 2.
  SORT lt_toav0 BY ar_date DESCENDING.

  IF lv_count > 0.
    CLEAR lt_toav0.
    READ TABLE lt_toav0 INDEX 1.
    REFRESH lt_bindata.
*    CALL FUNCTION 'ARCHIVOBJECT_GET_TABLE'
*      EXPORTING
*        archiv_id                = 'A2'
*        document_type            = 'HRICOLFOTO'
*        archiv_doc_id            = lt_toav0-arc_doc_id
*      IMPORTING
*        binlength                = lv_length
*      TABLES
*        binarchivobject          = lt_bindata
*      EXCEPTIONS
*        error_archiv             = 1
*        error_communicationtable = 2
*        error_kernel             = 3
*        OTHERS                   = 4.

    CALL FUNCTION 'SCMS_DOC_READ'
      EXPORTING
*       MANDT                 = SY-MANDT
        stor_cat              = space
        crep_id               = lt_toav0-archiv_id
        doc_id                = lt_toav0-arc_doc_id
      TABLES
        access_info           = lt_info
        content_bin           = lt_bin
      EXCEPTIONS
        bad_storage_type      = 1
        bad_request           = 2
        unauthorized          = 3
        comp_not_found        = 4
        not_found             = 5
        forbidden             = 6
        conflict              = 7
        internal_server_error = 8
        error_http            = 9
        error_signature       = 10
        error_config          = 11
        error_format          = 12
        error_parameter       = 13
        error                 = 14
        OTHERS                = 15.

    IF sy-subrc <> 0 AND lt_bin[] IS INITIAL.
      EXIT.
    ELSE.
      DATA:lv_len TYPE i,
           lv_lin TYPE i.
      CLEAR lv_bindata.
      CLEAR lv_len.
*      DESCRIBE TABLE lt_bindata LINES lv_lin.
*      lv_len = lv_length.

*      LOOP AT lt_bindata.
*        l_str = lt_bindata-line.
*        lv_len = lv_len + strlen(l_str).
*        CONCATENATE lv_bindata lt_bindata-line INTO lv_bindata IN BYTE MODE.
*      ENDLOOP.
      READ TABLE lt_info INTO ls_info INDEX 1.
      lv_len = ls_info-comp_size.
      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
        EXPORTING
          input_length = lv_len
        IMPORTING
          buffer       = lv_bindata
        TABLES
          binary_tab   = lt_bin[]
        EXCEPTIONS
          failed       = 1
          OTHERS       = 2.


*      CLEAR ev_photo.
*      CALL FUNCTION 'SSFC_BASE64_ENCODE'
*        EXPORTING
*          bindata = lv_bindata
*        IMPORTING
*          b64data = ev_photo.
**      REPLACE 'data:image/jpeg;base64,' IN ev_photo WITH space.
**      REPLACE 'data:image/jpg;base64,' IN ev_photo WITH space.
**      REPLACE
*      IF ev_photo IS NOT INITIAL.
*        EXIT.
*      ENDIF.

    ENDIF.


    host =   'http://xxxxxxxxxxxxxx/balabala/upload'.
    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        url                = host
      IMPORTING
        client             = client
      EXCEPTIONS
        argument_not_found = 1
        plugin_not_active  = 2
        internal_error     = 3
        OTHERS             = 4.

    CALL METHOD client->request->set_method(if_http_request=>co_request_method_post).

    CALL METHOD client->request->set_version(if_http_request=>co_protocol_version_1_0).

    CALL METHOD client->request->if_http_entity~set_content_type(
      EXPORTING
        content_type = 'multipart/form-data' ).

    CALL METHOD client->request->if_http_entity~set_formfield_encoding
      EXPORTING
        formfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw.
    part = client->request->if_http_entity~add_multipart( ).
    CALL METHOD part->set_header_field
      EXPORTING
        name  = 'content-disposition'
        value = 'form-data; name="content"; filename="god.jpg";'.

    CALL METHOD part->set_content_type
      EXPORTING
        content_type = 'application/x-jpg-compressed'.

    len = xstrlen(lv_bindata).

    CALL METHOD part->set_data
      EXPORTING
        data   = lv_bindata
        offset = 0
        length = len.
    CALL METHOD client->send
      EXPORTING
        timeout                    = 200
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3
        OTHERS                     = 4.

    IF sy-subrc NE 0.
      client->get_last_error(IMPORTING message = lv_error_msg).
    ENDIF.


    CALL METHOD client->receive
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3
        OTHERS                     = 4.

    IF sy-subrc NE 0.
      client->get_last_error(IMPORTING message = lv_error_msg).
    ENDIF.
    CLEAR:l_str.
    l_str = client->response->get_cdata( ).

    IF lv_error_msg IS INITIAL.
      es_message = VALUE #(msgtx = 'S' msgty = 'Send is ok').
    ENDIF.
  ENDIF.
ENDFUNCTION. 

正文完
 0