以采购订单中的附件下载为例
方法一:
* buseinss object key
DATA: ls_lpor TYPE sibflporb,
lv_document_id TYPE char46.
DATA: lt_links TYPE obl_t_link,
ls_links LIKE LINE OF lt_links,
lt_relat TYPE obl_t_relt,
la_relat LIKE LINE OF lt_relat.
DATA: lt_object_content TYPE STANDARD TABLE OF solisti1,
ls_documentdata TYPE sofolenti1,
ls_object_content TYPE solisti1.
DATA: lv_filename TYPE string.
DATA: lo_root TYPE REF TO cx_root.
ls_lpor-instid = ''.
ls_lpor-typeid = 'BUS2012'.
ls_lpor-catid = 'BO'.
la_relat-sign = 'I'.
la_relat-option = 'EQ'.
la_relat-low = 'ATTA'. "Attachements
APPEND la_relat TO lt_relat.
* Read the links
TRY.
CALL METHOD cl_binary_relation=>read_links
EXPORTING
is_object = ls_lpor
it_relation_options = lt_relat
IMPORTING
et_links = lt_links.
CATCH cx_obl_parameter_error INTO lo_root.
CATCH cx_obl_internal_error INTO lo_root.
CATCH cx_obl_model_error INTO lo_root.
ENDTRY.
LOOP AT lt_links INTO ls_links.
lv_document_id = ls_links-instid_b .
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
EXPORTING
document_id = lv_document_id
* FILTER = 'X '
IMPORTING
document_data = ls_documentdata
TABLES
* OBJECT_HEADER =
object_content = lt_object_content
* OBJECT_PARA =
* OBJECT_PARB =
* ATTACHMENT_LIST =
* RECEIVER_LIST =
* CONTENTS_HEX =
EXCEPTIONS
document_id_not_exist = 1
operation_no_authorization = 2
x_error = 3
OTHERS = 4.
IF sy-subrc = 0.
lv_filename = 'D:\' && ls_documentdata-obj_descr.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE = BIN_FILESIZE
filename = lv_filename
filetype = 'BIN'
* IMPORTING
* FILELENGTH = FILELENGTH
TABLES
data_tab = lt_object_content
* FIELDNAMES = FIELDNAMES
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21.
ENDIF.
ENDLOOP.
方法二:
DATA: lv_filename TYPE string.
DATA: lo_root TYPE REF TO cx_root.
DATA: lv_objtype TYPE objecttypeid,
lv_objkey TYPE objky,
lt_att_content TYPE if_odata_cv_attachment_api=>ty_t_attachment_content,
lt_messages TYPE bapiret2_t.
DATA: lv_output_length TYPE i,
lt_binary_tab TYPE TABLE OF solisti1.
lv_objtype = 'BUS2012'.
lv_objkey = ''.
TRY.
cl_odata_cv_attachment_api=>get_instance( )->if_odata_cv_attachment_api~retrieve_attachments_content( " to retrive attachment content
EXPORTING
iv_objecttype_long = lv_objtype
iv_objectkey = lv_objkey
iv_run_no_auth_check = abap_true
IMPORTING
et_attachment_contents = lt_att_content
et_messages = lt_messages
).
LOOP AT lt_att_content INTO DATA(ls_att_content).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = ls_att_content-attachment_content
IMPORTING
output_length = lv_output_length
TABLES
binary_tab = lt_binary_tab.
lv_filename = 'D:\' && ls_att_content-filename.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
BIN_FILESIZE = lv_output_length
filename = lv_filename
filetype = 'BIN'
* IMPORTING
* FILELENGTH = FILELENGTH
TABLES
data_tab = lt_binary_tab
* FIELDNAMES = FIELDNAMES
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21.
ENDLOOP.
CATCH cx_odata_cv_base_exception INTO lo_root.
ENDTRY.
使用函数ARCHIV_GET_CONNECTIONS,BDS_GOS_CONNECTIONS_GET等也可以实现。
其它业务对象的附件,主要区别在与参数typeid ,如果对工作流较为熟悉,就能发现此处参数即为对应的工作流BOR(在hana中有些使用的是对象类)