ABAP:订单附件GOS上传/下载/读取功能
GOS附件上传
常用的几种对象类型:
采购订单:BUS2012
销售订单:BUS2032
DN交货单:LIKP
销售发票:VBRK
采购发票:BUS2081
会计凭证:BUS6035
可以在TOJTT表中查找相关对象类型
*&---------------------------------------------------------------------* *& Report ZTEST_GOS *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ztest_gos. START-OF-SELECTION. PERFORM gos_upload USING 'BUS2012' '4500000105' 'C:\Users\SC10516\Desktop\新建文本文档.txt'. *&---------------------------------------------------------------------* *& Form GOS_UPLOAD *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_0007 text * -->P_0008 text * -->P_0009 text * <--P_SY_SUBRC text *----------------------------------------------------------------------* FORM gos_upload USING p_objtype p_objkey p_filestr. DATA : obj_roleb TYPE borident, obj_rolea TYPE borident, document LIKE sood4, folder_id LIKE soodk, header_data LIKE sood2, folmem_data LIKE sofm2, receive_data LIKE soos6. DATA : BEGIN OF lt_files OCCURS 0, filename(128), END OF lt_files. *** 获取文件夹根ID CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET' EXPORTING owner = ' ' region = 'B' IMPORTING folder_id = folder_id EXCEPTIONS communication_failure = 1 owner_not_exist = 2 system_failure = 3 x_error = 4. CLEAR lt_files[]. APPEND p_filestr TO lt_files. *** 取上传文件的标题作为附件的剪短描述 CALL FUNCTION 'J_3RVATD_SPLIT_FILENAME' EXPORTING iv_long_filename = p_filestr IMPORTING ev_pure_filename = header_data-objdes * EV_PURE_EXTENSION = . *** 对象文件夹:来自 ID 的对象类型 document-foltp = folder_id-objtp. *** 对象文件夹:来自 ID 的年 document-folyr = folder_id-objyr. *** 对象文件夹:来自 ID 的编号 document-folno = folder_id-objno. *** 内容的简短描述 document-objdes = header_data-objdes. *** 文档,文件夹或分配清单的名称 document-objnam = header_data-objdes. CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER' EXPORTING method = 'IMPORTFROMPC' * OFFICE_USER = * REF_DOCUMENT = * NEW_PARENT = * IMPORTING * AUTHORITY = * RCODE = TABLES * OBJCONT = * OBJHEAD = * OBJPARA = * OBJPARB = * RECIPIENTS = * ATTACHMENTS = * REFERENCES = files = lt_files[] CHANGING document = document header_data = header_data folmem_data = folmem_data receive_data = receive_data. IF document-okcode = 'CREA' OR document-okcode = 'CHNG'. *** 对象关键字 obj_rolea-objkey = p_objkey. *** 对象类型 obj_rolea-objtype = p_objtype. *** 对象类型 obj_roleb-objtype = 'MESSAGE'. *** 对象关键字 obj_roleb-objkey = document(34). CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT' EXPORTING obj_rolea = obj_rolea obj_roleb = obj_roleb relationtype = 'ATTA' * IMPORTING * BINREL = * TABLES * BINREL_ATTRIB = EXCEPTIONS no_model = 1 internal_error = 2 unknown = 3. IF sy-subrc = 0. WRITE : 'Upload success.'. ELSE. WRITE : 'Upload fail.'. ENDIF. ELSE. WRITE : 'Create fail.'. ENDIF. ENDFORM. " GOS_UPLOAD
GOS下载
*&---------------------------------------------------------------------* *& Report ZTEST_GOS1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ztest_gos1. START-OF-SELECTION. PERFORM gos_download USING 'BUS2012' '4500000105' 'D:\说明文档.text'. *&---------------------------------------------------------------------* *& Form GOS_DOWNLOAD *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_0007 text * -->P_0008 text * -->P_0009 text *----------------------------------------------------------------------* FORM gos_download USING p_classname p_ebeln p_filestr. DATA : BEGIN OF itab_srgb OCCURS 0. INCLUDE STRUCTURE srgbtbrel. DATA : filename(200), END OF itab_srgb. DATA : objcont TYPE TABLE OF soli, folder_id LIKE soodk, object_id LIKE soodk, loio_object LIKE sdokobject, context LIKE sdokpropty OCCURS 0 WITH HEADER LINE, phio_object LIKE sdokobject, file_access_info LIKE sdokfilaci OCCURS 0 WITH HEADER LINE, file_content_ascii LIKE sdokcntasc OCCURS 0 WITH HEADER LINE, cont_bin LIKE sdokcntbin OCCURS 0 WITH HEADER LINE. SELECT * FROM srgbtbrel INTO TABLE itab_srgb WHERE instid_a = p_ebeln AND typeid_a = p_classname AND catid_a = 'BO' AND reltype = 'ATTA' AND logsys_a = ''. READ TABLE itab_srgb INDEX 1. folder_id = itab_srgb-instid_b+0(17). object_id = itab_srgb-instid_b+17(17). CALL FUNCTION 'SO_OBJECT_READ' EXPORTING folder_id = folder_id object_id = object_id TABLES objcont = objcont EXCEPTIONS active_user_not_exist = 1 communication_failure = 2 component_not_available = 3 folder_not_exist = 4 folder_no_authorization = 5 object_not_exist = 6 object_no_authorization = 7 operation_no_authorization = 8 owner_not_exist = 9 parameter_error = 10 substitute_not_active = 11 substitute_not_defined = 12 system_failure = 13 x_error = 14. IF sy-subrc <> 0. EXIT. ENDIF. CALL FUNCTION 'SO_KPRO_DATA_FROM_OBJCONT_GET' IMPORTING loio_object = loio_object TABLES objcont = objcont context = context EXCEPTIONS missing_kpro_data = 1. IF sy-subrc = 0 . CALL FUNCTION 'SO_LOIO_PHIO_GET' EXPORTING loio_object = loio_object IMPORTING phio_object = phio_object TABLES context = context EXCEPTIONS kpro_inconsistency = 1 x_error = 2. CALL FUNCTION 'SDOK_PHIO_LOAD_CONTENT' EXPORTING object_id = phio_object text_as_stream = 'X' TABLES file_access_info = file_access_info file_content_ascii = file_content_ascii file_content_binary = cont_bin EXCEPTIONS not_existing = 1 not_authorized = 2 no_content = 3 bad_storage_type = 4. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = p_filestr filetype = 'BIN' TABLES data_tab = cont_bin 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. ELSE. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = p_filestr filetype = 'ASC' TABLES data_tab = objcont 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. IF sy-subrc = 0 . WRITE : 'Download success.'. ELSE. WRITE : 'Download fail.'. ENDIF. * DATA: * OWN_LOGICAL_SYSTEM LIKE TBDLS-LOGSYS, * CLASSNAME LIKE BAPIBDS01-CLASSNAME, * GOS_OBJKEY LIKE SWOTOBJID-OBJKEY, * LT_GOS_CONNECTIONS LIKE BDN_CON OCCURS 1 WITH HEADER LINE, * LV_ID TYPE SOFOLENTI1-DOC_ID, * LS_DOC_DATA TYPE SOFOLENTI1, * LT_OBJECT_HEADER TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE, * LT_OBJECT_CONTENT TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE, * LT_HEX TYPE STANDARD TABLE OF SOLIX WITH HEADER LINE. * **** 获得当前的SAP系统名 * CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET' * IMPORTING * OWN_LOGICAL_SYSTEM = OWN_LOGICAL_SYSTEM * EXCEPTIONS * OWN_LV_LOGICAL_SYSTEM_NOT_DEFINED = 1 * OTHERS = 2. * **** 对象关键字 * GOS_OBJKEY = P_EBELN. * **** 获取GOS的链接 * CALL FUNCTION 'BDS_GOS_CONNECTIONS_GET' * EXPORTING * LOGICAL_SYSTEM = OWN_LOGICAL_SYSTEM * CLASSNAME = P_CLASSNAME * OBJKEY = GOS_OBJKEY * CLIENT = SY-MANDT * TABLES * GOS_CONNECTIONS = LT_GOS_CONNECTIONS * EXCEPTIONS * NO_OBJECTS_FOUND = 1 * INTERNAL_ERROR = 2 * INTERNAL_GOS_ERROR = 3 * OTHERS = 4. * * LOOP AT LT_GOS_CONNECTIONS. * **** 文件夹条目 ID(对象+文件夹+转发者姓名) * LV_ID = LT_GOS_CONNECTIONS-LOIO_ID. * **** 通过GOS连接读取具体的附件文档名称、内容等相关信息 * CALL FUNCTION 'SO_DOCUMENT_READ_API1' * EXPORTING * DOCUMENT_ID = LV_ID * IMPORTING * DOCUMENT_DATA = LS_DOC_DATA * TABLES * OBJECT_HEADER = LT_OBJECT_HEADER * OBJECT_CONTENT = LT_OBJECT_CONTENT * CONTENTS_HEX = LT_HEX * EXCEPTIONS * DOCUMENT_ID_NOT_EXIST = 1 * OPERATION_NO_AUTHORIZATION = 2 * X_ERROR = 3 * OTHERS = 4. * BREAK-POINT. * * ENDLOOP. ENDFORM. " GOS_DOWNLOAD
GOS上传URL
FUNCTION ZFM_SD_CREATE_URL . *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" VALUE(IS_INFO) TYPE ZSSD0022 OPTIONAL *" TABLES *" IT_URL STRUCTURE ZSSD0020 OPTIONAL *" ET_RETURN STRUCTURE ZSSD0023 OPTIONAL *"---------------------------------------------------------------------- DATA:LV_OBJTYP TYPE SWO_OBJTYP. "(table BDS_LOCL) DATA:FOLDER_ID TYPE SOFDK. DATA LV_URL_ID TYPE SO_URL. DATA:LT_OBJCONT TYPE TABLE OF SOLI, LS_OBJCONT TYPE SOLI. DATA:LT_OBJHEAD TYPE TABLE OF SOLI, LS_OBJHEAD TYPE SOLI. DATA:LS_OBJECT_HD TYPE SOOD1, LS_OBJECT_ID TYPE SOODK. DATA:DOCUMENT_ID TYPE SOFMK, LS_ROLEA TYPE BORIDENT, LS_ROLEB TYPE BORIDENT. DATA:LV_MSG TYPE STRING. CASE IS_INFO-ZBUTYPE. WHEN 'S'. "SO LV_OBJTYP = 'BUS2032'. WHEN 'D'. "DN LV_OBJTYP = 'LIKP'. WHEN 'B'. "BILL LV_OBJTYP = 'VBRK'. WHEN 'R'. LV_OBJTYP = 'BUS2081'. WHEN 'X'."PR LV_OBJTYP = 'BUS2105'. WHEN OTHERS. ET_RETURN-TYPE = 'E'. ET_RETURN-MESSAGE = '不支持当前业务类型'. APPEND ET_RETURN. RETURN. ENDCASE. CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET' EXPORTING REGION = 'B' IMPORTING FOLDER_ID = FOLDER_ID EXCEPTIONS OTHERS = 1. LOOP AT IT_URL INTO DATA(LS_URL). CLEAR:LV_URL_ID,LS_OBJECT_HD,LS_OBJECT_ID,LT_OBJCONT,LT_OBJHEAD, LS_ROLEA,LS_ROLEB,LV_MSG. LV_URL_ID = LS_URL-URL. WHILE NOT LV_URL_ID IS INITIAL. CONCATENATE '&KEY&' LV_URL_ID(250) INTO LS_OBJCONT. APPEND LS_OBJCONT TO LT_OBJCONT. SHIFT LV_URL_ID LEFT BY 250 PLACES. ENDWHILE. LS_OBJECT_HD-OBJDES = LS_URL-TITLE. LS_OBJECT_HD-OBJSNS = 'O'. LS_OBJECT_HD-OBJLA = SY-LANGU. CALL FUNCTION 'SO_OBJECT_INSERT' EXPORTING FOLDER_ID = FOLDER_ID * OBJECT_FL_CHANGE = ' ' OBJECT_HD_CHANGE = LS_OBJECT_HD OBJECT_TYPE = 'URL' * ORIGINATOR_ID = ' ' * OWNER = ' ' IMPORTING * OBJECT_FL_DISPLAY = * OBJECT_HD_DISPLAY = OBJECT_ID = LS_OBJECT_ID TABLES OBJCONT = LT_OBJCONT OBJHEAD = LT_OBJHEAD * OBJPARA = * OBJPARB = EXCEPTIONS ACTIVE_USER_NOT_EXIST = 1 COMMUNICATION_FAILURE = 2 COMPONENT_NOT_AVAILABLE = 3 DL_NAME_EXIST = 4 FOLDER_NOT_EXIST = 5 FOLDER_NO_AUTHORIZATION = 6 OBJECT_TYPE_NOT_EXIST = 7 OPERATION_NO_AUTHORIZATION = 8 OWNER_NOT_EXIST = 9 PARAMETER_ERROR = 10 SUBSTITUTE_NOT_ACTIVE = 11 SUBSTITUTE_NOT_DEFINED = 12 SYSTEM_FAILURE = 13 X_ERROR = 14 OTHERS = 15. IF SY-SUBRC = 0. DOCUMENT_ID-FOLTP = FOLDER_ID-FOLTP. DOCUMENT_ID-FOLYR = FOLDER_ID-FOLYR. DOCUMENT_ID-FOLNO = FOLDER_ID-FOLNO. DOCUMENT_ID-DOCTP = LS_OBJECT_ID-OBJTP. DOCUMENT_ID-DOCYR = LS_OBJECT_ID-OBJYR. DOCUMENT_ID-DOCNO = LS_OBJECT_ID-OBJNO. LS_ROLEA-OBJKEY = IS_INFO-OBJKEY. LS_ROLEA-OBJTYPE = LV_OBJTYP. LS_ROLEB-OBJKEY = DOCUMENT_ID . LS_ROLEB-OBJTYPE = 'MESSAGE' . CALL FUNCTION 'BINARY_RELATION_CREATE' EXPORTING OBJ_ROLEA = LS_ROLEA " IS_OBJECT OBJ_ROLEB = LS_ROLEB "REL_DOC RELATIONTYPE = 'URL' "'NOTE' EXCEPTIONS OTHERS = 1. IF SY-SUBRC <> 0. ROLLBACK WORK. ET_RETURN-TITLE = LS_URL-TITLE. ET_RETURN-TYPE = 'E'. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 INTO LV_MSG. ET_RETURN-MESSAGE = LV_MSG. APPEND ET_RETURN. CLEAR ET_RETURN. CONTINUE. ELSE. COMMIT WORK. ET_RETURN-TITLE = LS_URL-TITLE. ET_RETURN-TYPE = 'S'. APPEND ET_RETURN. CLEAR ET_RETURN. CONTINUE. ENDIF. ELSE. ROLLBACK WORK. ET_RETURN-TITLE = LS_URL-TITLE. ET_RETURN-TYPE = 'E'. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 INTO LV_MSG. ET_RETURN-MESSAGE = LV_MSG. APPEND ET_RETURN. CLEAR ET_RETURN. CONTINUE. ENDIF. ENDLOOP. ENDFUNCTION.
GOS附件读取
PARAMETERS:p_ebeln TYPE ekko-ebeln. DATA:lv_logical_system LIKE bapibds01-log_system, classname LIKE bapibds01-classname, gos_objkey LIKE swotobjid-objkey, lt_gos_connections LIKE bdn_con OCCURS 1 WITH HEADER LINE. DATA:lv_id TYPE sofolenti1-doc_id, ls_doc_data TYPE sofolenti1, lt_object_header TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE, lt_object_content TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE, lt_hex TYPE STANDARD TABLE OF solix WITH HEADER LINE. CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET' IMPORTING own_logical_system = lv_logical_system EXCEPTIONS own_lv_logical_system_not_defined = 1 OTHERS = 2. . gos_objkey = p_ebeln. "采购订单 CALL FUNCTION 'BDS_GOS_CONNECTIONS_GET' EXPORTING logical_system = lv_logical_system classname = 'BUS2012' "用来标识采购订单 objkey = gos_objkey client = sy-mandt TABLES gos_connections = lt_gos_connections EXCEPTIONS no_objects_found = 1 internal_error = 2 internal_gos_error = 3 OTHERS = 4. LOOP AT lt_gos_connections. lv_id = lt_gos_connections-loio_id. CALL FUNCTION 'SO_DOCUMENT_READ_API1' EXPORTING document_id = lv_id IMPORTING document_data = ls_doc_data TABLES object_header = lt_object_header object_content = lt_object_content contents_hex = lt_hex EXCEPTIONS document_id_not_exist = 1 operation_no_authorization = 2 x_error = 3 OTHERS = 4. BREAK-POINT. ENDLOOP.
GOS列表展示
DATA:is_object TYPE sibflporb, ip_mode TYPE char1. is_object-instid = '9000001787'."实例标识,例如:公司代码+凭证编号+会计年度 is_object-typeid = 'VBRK'."对象类型 is_object-catid = 'BO'."默认 ip_mode = 'D'. "D仅显示 *E可更改但不提交数据库更新 *C可更改并提交数据库更新 CALL FUNCTION 'GOS_ATTACHMENT_LIST_POPUP' EXPORTING is_object = is_object ip_check_arl = 'X' ip_check_bds = 'X' ip_notes = 'X' ip_attachments = 'X' ip_urls = 'X' ip_mode = ip_mode. * IMPORTING * EP_SAVE_REQUEST = * TABLES * IT_OBJECTS =