Code listing for: UPLOAD_BINARY_FILE

Description: Upload binary file and attach to given GOS Object

**************************************************************************
*   Method attributes.                                                   *
**************************************************************************
Instantiation: Public
**************************************************************************

METHOD upload_binary_file.

  FIELD-SYMBOLS: <fs> TYPE any.

  DATA:
    ls_business_object TYPE sibflporb,
    ls_content TYPE sdokcntbin,
    lt_content TYPE sdokcntbins,
    lv_strlen TYPE i,
    ls_file_access_info TYPE sdokfilaci,
    lt_file_access_info TYPE sdokfilacis,
    ls_properties TYPE sdokpropty,
    lt_properties TYPE sdokproptys.

  DATA:
    lv_offset TYPE i,
    lv_bytes_rm TYPE i,
    lv_bytes_rd TYPE i,
    lv_content_type TYPE w3conttype,
    lv_filepath TYPE string,
    lv_fileext TYPE string,
    lv_filename TYPE string.

  DATA:
    ls_logical_key TYPE skwf_io,
    ls_physical_key TYPE skwf_io,
    ls_key TYPE skwf_lpio,
    ls_error TYPE skwf_error,
    lv_dummy_message TYPE string.

  ls_business_object-instid = object_key.
  ls_business_object-typeid = object_type.
  ls_business_object-catid = category_id.

  lv_strlen = xstrlen( binary_string ).

* Split the file path
  CALL FUNCTION 'CRM_KW_SPLIT_FILENAME'
    EXPORTING
      iv_path      = filename
    IMPORTING
      ev_filename  = lv_filename
      ev_extension = lv_fileext
      ev_mimetype  = lv_content_type.

* Assign file properties
  ls_file_access_info-file_size  = lv_strlen.
  ls_file_access_info-binary_flg = 'X'.
  ls_file_access_info-file_name  = lv_filename.
  ls_file_access_info-mimetype   = lv_content_type.
  ls_file_access_info-property   = lv_fileext.
  APPEND ls_file_access_info TO lt_file_access_info.

  ls_properties-name  = 'KW_RELATIVE_URL'.
  ls_properties-value = lv_filename.
  APPEND ls_properties TO lt_properties.

  ls_properties-name  = 'DESCRIPTION'.
  ls_properties-value = lv_filename.
  APPEND ls_properties TO lt_properties.

  ls_properties-name  = 'LANGUAGE'.
  ls_properties-value = sy-langu.
  APPEND ls_properties TO lt_properties.

* The entire file content is stored in variable lv_str as a single string
* Split the string into 1022 chars and append to internal table lt_content.
  lv_offset   = 1022.
  lv_bytes_rd = 0.
  lv_bytes_rm = lv_strlen.

  ASSIGN binary_string TO <fs>.

  WHILE lv_bytes_rd LT lv_strlen.
    IF  lv_bytes_rm LT lv_offset.
      ls_content-line = <fs>+lv_bytes_rd(lv_bytes_rm).
    ELSE.
      ls_content-line = <fs>+lv_bytes_rd(lv_offset).
    ENDIF.
    APPEND ls_content TO lt_content.
    lv_bytes_rd = lv_bytes_rd + lv_offset.
    lv_bytes_rm = lv_strlen - lv_bytes_rd.
    CLEAR ls_content.
  ENDWHILE.

* Create the document
  CALL METHOD cl_crm_documents=>create_with_table
    EXPORTING
      business_object     = ls_business_object
      properties          = lt_properties
      file_access_info    = lt_file_access_info
      file_content_binary = lt_content
      raw_mode            = 'X'
    IMPORTING
      loio                = ls_logical_key
      phio                = ls_physical_key
      error               = ls_error.

  IF ls_error IS NOT INITIAL.
    MESSAGE ID ls_error-id TYPE ls_error-type NUMBER ls_error-no
     WITH ls_error-v1 ls_error-v2 ls_error-v3 ls_error-v4
     INTO lv_dummy_message.
    RAISE EXCEPTION TYPE zcx_gos_attachment_exception.
  ENDIF.

  IF ls_error IS INITIAL AND ls_logical_key IS NOT INITIAL.

    ls_key-objtypelo = ls_logical_key-objtype.
    ls_key-classlo = ls_logical_key-class.
    ls_key-objidlo = ls_logical_key-objid.
    ls_key-objtypeph = ls_physical_key-objtype.
    ls_key-classph = ls_physical_key-class.
    ls_key-objidph = ls_physical_key-objid.

    CREATE OBJECT attachment
      EXPORTING
        key = ls_key.
  ENDIF.

ENDMETHOD.