SAP GOS document download (第2页)

call method zcl_oh_my_gos=>gos_get_file_list     exporting       objtype       = p_type       objkey        = p_key     importing       t_attachments = lta_sood     changing       es_return     = t_st_return.

if t_st_return-code is initial.     data dec_kb type p.

loop at lta_sood into lwa_sood.       call method zcl_oh_my_gos=>gos_download_file         exporting           file_path  = ‘C:\TEMP’           attachment = lwa_sood         changing           es_return  = t_st_return.       if t_st_return-code is initial.         dec_kb = lwa_sood-objlen / 1024.         if dec_kb < 1.           dec_kb = 1.         endif.         write: / lwa_sood-objdes, dec_kb, ‘KB’.       else.         message t_st_return-text type ‘W’.       endif.     endloop.   else.     message t_st_return-text type ‘W’.   endif.

Alert Moderator

Assigned tags

ABAP Development

attachments

bds gos connections get

download

generic object services

View more...

Related Blog Posts

Everything GOS (Generic Object Services)

By Philip Johnston, May 23, 2013

How to add Generic Object Services to your context menus

By Former Member, Aug 28, 2015

Using the GOS (Generic Object Services) Class within ITS programs

By Philip Johnston, May 23, 2013

Related Questions

Generic Object Services

By Former Member, Aug 11, 2008

Generic Object Services

By Former Member, Jan 08, 2008

Generic Object Services

By Former Member, Dec 20, 2007

14 Comments

You must be Logged on to comment or reply to a post.

Former Member

December 27, 2012 at 3:35 pm

I´d have add a method to "download" the file into an xString so it´d be possible to download it from a BSP, for example.

This is the code I made to do so:

METHOD gos_download_file_web.

DATA: attachment       TYPE sood,

ex               TYPE REF TO cx_root,

ltp_sortfield    TYPE char30,

lta_objcont      TYPE soli_tab,

temp_objcont     TYPE soli,

temp_loio_id     TYPE sdok_loid,

wa_soffphio      TYPE soffphio,

objectid         TYPE sdokobject,

documents        TYPE TABLE OF soffphf,

document         TYPE soffphf,

filecontent      TYPE TABLE OF sdokcntbin,

file_access_info TYPE TABLE OF sdokfilaci,

wa_fai           TYPE sdokfilaci,

temp_len         TYPE i,

temp_fln         TYPE i,

temp_lln         TYPE i.

TRY.

READ TABLE t_attachments INTO attachment INDEX attch_num.

CONCATENATE attachment-objtp attachment-objyr attachment-objno INTO ltp_sortfield.

IMPORT objcont_tab TO lta_objcont FROM DATABASE soc3(dt) ID ltp_sortfield.

IF sy-subrc = 0.

READ TABLE lta_objcont INTO temp_objcont INDEX 1.

temp_loio_id = temp_objcont+27(32).

SELECT SINGLE * FROM soffphio INTO wa_soffphio

WHERE loio_id = temp_loio_id.

SELECT SINGLE * FROM soffphf INTO document

WHERE phio_id = wa_soffphio-phio_id.

IF sy-subrc EQ 0.

mimetype = document-mimetype.

IF mimetype IS INITIAL.

CASE attachment-file_ext.

WHEN 'DOC' OR 'doc'.

mimetype = 'application/msword'.

WHEN 'PDF' OR 'pdf'.

mimetype = 'application/pdf'.

ENDCASE.

ENDIF.

objectid-class = document-ph_class.

objectid-objid = document-phio_id.

CALL FUNCTION 'SDOK_PHIO_LOAD_CONTENT'

EXPORTING

object_id           = objectid

TABLES

file_access_info    = file_access_info

file_content_binary = filecontent

EXCEPTIONS

not_existing        = 1

not_authorized      = 2

no_content          = 3

bad_storage_type    = 4

OTHERS              = 5.

READ TABLE file_access_info INTO wa_fai INDEX 1.

temp_len = wa_fai-file_size.

temp_fln = wa_fai-first_line.

temp_lln = wa_fai-last_line.

CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'

EXPORTING

input_length = temp_len

first_line   = temp_fln

last_line    = temp_lln

IMPORTING

buffer       = docxstring

TABLES

binary_tab   = filecontent.

IF sy-subrc = 0.

es_return = 0.

ENDIF.

ENDIF.

ENDIF.

CATCH cx_root INTO ex.

es_return = 3.

ENDTRY.

ENDMETHOD.

Has anyone any advice to improve it?? Anyway, it works...

This article has been VERY useful.

Thanks a lot.

Like(0)

Philip Johnston Post author

December 28, 2012 at 12:28 am

I like it.  Thanks for sharing..

Like(0)

Former Member

January 16, 2013 at 9:58 pm

Hi experts,

Maybe you can help me with a big challenge here. I need to migrate some materials attachment (MM03), these materials have been migrated from SAP to SAP systems and now I need to put them attachments massively. The two SAP systems are connected through RFC.

I used some FM as BDS_GOS_CONNECTIONS_GET and SO_DOCUMENT_READ_API1 to get the attachment in binary mode. So at this time I don’t know how to insert these

attachments in another SAP system.

So, how to insert these attachments in the corresponding material through RFC? I did not find a GOS FM to insert it.

Thank you in advanced

Antonio Rocha

Like(0)

Philip Johnston Post author

January 17, 2013 at 3:48 pm

Antonio, you can use the Methods above to solve you challenge.  There are two ways you can approach:

1.  Use the GOS_GET_FILE_LIST method and loop through results.  In your loop,

call method GOS_DOWNLOAD_FILE to save all files to a folder on your hard

drive.

Then in your receiving system, call WS_UPLOAD and loop through all the files

calling method GOS_ATTACH_FILE to put the files into the receiving system.

2.   Since you mentioned the systems are connected, I would use a similar

approach but use the RFC destination option on your function calls so you can

bypass the download to hard drive step.

Should be fairly easy to do.

thanks, Phil

Like(0)

Former Member

January 17, 2013 at 4:53 pm

Thanks for your help, Philip. I will get an abaper and We'll try to do that through RFC approach.

Antonio

Like(0)

Former Member

March 26, 2014 at 3:11 pm

Hello Philip,

Do you know if the method to attach files into GOS transactions works in backgroud mode also?

Thanks!

Like(0)

Philip Johnston Post author

March 26, 2014 at 3:41 pm

Yes.  it can be used for background batch uploads.

Like(0)

Ramya g

August 30, 2016 at 5:19 pm

Hello Philip,

I have a PO number at my report output and I want to add attachments (attachment present at presentation server ) from my report to the PO level. Please suggest me only adding the attachment from the report to the PO level.

Thanks,

RG

Like(0)

Nicolae Cadar

August 2, 2018 at 11:12 am

Hi, can you show the definition on SE11 of your table :ZVNT_ST_RETURN  and Z_TT_SOLI  i would be very helpfull i know its a couple of years later but i would like you see it if it's possible.

Thank you in advance.

Andrei Cadar

Like(0)

Kanishak Gupta

May 27, 2019 at 1:15 pm

Hi Philip,

Thanks for your document. It really helps.

Could you please let me know and provide help if you have worked on creating URLs?

regards

kanishak

Like(0)

Paul Wieland

August 19, 2019 at 5:30 pm

Thank you for the class, it was exactly what I needed.  Have you considered opening a repo to host it on github?

Like(0)

Dharmesh Kumar

February 5, 2020 at 12:12 am

Thank you for an excellent blog mate, it is really helpful.

Could you please also explain the structure of custom objects  ZVNT_ST_RETURN  and Z_TT_SOLI.

Regards,

Dharmesh

Like(0)

Dharmesh Kumar

February 5, 2020 at 1:32 am

I have used the below structure and was able to successfully execute this above code.

ZVNT_ST_RETURN

Z_TT_SOLI -> use this table type instead SOLI_TAB

Method --> LOG_MSG

Like(0)

Suprimentos BrasKem

April 16, 2020 at 6:58 am

The entire code, in a report, for testing . Just copy and paste in a report. Only that.

report x.

class lcl definition.

public section.

types: ty_ysood_tab type standard table of sood with default key.

types: begin of ty_yreturn,

type type char1,

text type string,

end of ty_yreturn.

class-methods: gos_get_file_list

importing objtype type swo_objtyp

objkey type swo_typeid

exporting

t_attachments type ty_ysood_tab

changing es_return type ty_yreturn,

gos_attach_file

importing objtype type swo_objtyp

objkey type swo_typeid

folder_region type so_fol_rg

file_name_with_path type avwctxcont

file_name_no_path type avwctxcont OPTIONAL

exporting

es_return type ty_yreturn

changing

it_content type SOLI_TAB,

* it_content type ty_ysood_tab,

gos_download_file

importing

file_path type char100

attachment type sood

changing

es_return type ty_yreturn.

endclass.

parameters: p_key type swo_typeid obligatory default '000000000000000003',

p_type type swo_objtyp obligatory default 'BUS1001006',

p_file type rlgrap-filename obligatory.

parameters : r_upload radiobutton group g1,

r_down radiobutton group g1,

r_list radiobutton group g1 default 'X'.

at selection-screen on value-request for p_file.

call function 'F4_FILENAME'

exporting

program_name = syst-cprog

dynpro_number = syst-dynnr

field_name = 'P_FILE'

importing

file_name = p_file.

start-of-selection.

case abap_true.

when r_upload.

perform upload.

when r_down.

perform download.

when r_list.

perform list.

endcase.

class lcl implementation.

method gos_get_file_list.

types: begin of ts_key,

foltp type so_fol_tp,

folyr type so_fol_yr,

folno type so_fol_no,

objtp type so_obj_tp,

objyr type so_obj_yr,

objno type so_obj_no,

forwarder type so_usr_nam,

end of ts_key,

begin of ts_attachment,

foltp type so_fol_tp,

folyr type so_fol_yr,

folno type so_fol_no,

objtp type so_obj_tp,

objyr type so_obj_yr,

objno type so_obj_no,

brelguid type oblguid32,

roletype type oblroltype,

end of ts_attachment,

tt_attachment type table of ts_attachment.

data: ta_srgbtbrel type standard table of srgbtbrel,

wa_srgbtbrel type srgbtbrel,

lta_sood type standard table of sood,

lwa_sood type sood, ltp_pathin(1000) type c,

ltp_filename type string,

ltp_sortfield type char30,

lta_objcont type soli_tab,

lta_attachments type tt_attachment,

lwa_attachments like line of lta_attachments,

lo_boritem type ref to cl_sobl_bor_item,

lo_al_item type ref to cl_gos_al_item,

li_link type ref to if_browser_link,

ls_option type obl_s_relt,

lt_options type obl_t_relt,

ls_key type ts_key,

ls_attachment type ts_attachment,

lt_attachment type tt_attachment,

lt_links type obl_t_link,

ls_link type obl_s_link,

lp_linkid type blnk_inst,

gs_lpor type sibflporb.

if not objtype is initial and not objkey is initial.

select * from srgbtbrel into table ta_srgbtbrel

where instid_a eq objkey

and typeid_a eq objtype

and catid_a eq 'BO'.

if sy-subrc eq 0.

sort ta_srgbtbrel by instid_a typeid_a catid_a.

delete adjacent duplicates from ta_srgbtbrel comparing instid_a typeid_a catid_a.

loop at ta_srgbtbrel into wa_srgbtbrel.

clear: lt_attachment[], lta_attachments[].

gs_lpor-instid = wa_srgbtbrel-instid_a.

gs_lpor-typeid = wa_srgbtbrel-typeid_a.

gs_lpor-catid = wa_srgbtbrel-catid_a.

ls_option-sign = 'I'.

ls_option-option = 'EQ'.

ls_option-low = 'ATTA'.

append ls_option to lt_options.

ls_option-low = 'NOTE'.

append ls_option to lt_options.

ls_option-low = 'URL'.

append ls_option to lt_options.

try.

call method cl_binary_relation=>read_links_of_binrels

exporting

is_object = gs_lpor

it_relation_options = lt_options

ip_role = 'GOSAPPLOBJ'

importing

et_links = lt_links.

loop at lt_links into ls_link.

case ls_link-typeid_b .

when 'MESSAGE'.

ls_key = ls_link-instid_b.

move-corresponding ls_key to ls_attachment.

ls_attachment-roletype = ls_link-roletype_b.

if ls_link-brelguid is initial.

ls_attachment-brelguid = ls_link-relguidold.

else.

ls_attachment-brelguid = ls_link-brelguid.

endif.

append ls_attachment to lt_attachment.

when others.

continue.

endcase.

endloop.

catch cx_obl_parameter_error .

catch cx_obl_internal_error .

catch cx_obl_model_error .

catch cx_root.

endtry.

endloop.

lta_attachments[] = lt_attachment[].

check lines( lta_attachments ) > 0.

select * from sood into table lta_sood

for all entries in lta_attachments

where

objtp = lta_attachments-objtp and

objyr = lta_attachments-objyr and

objno = lta_attachments-objno.

if sy-subrc eq 0.

t_attachments[] = lta_sood.

es_return-type = 'S'.

es_return-text = 'SUCCESS'.

endif.

data rcode type i.

data objhead_tab type table of soli.

data objcont_tab type table of soli.

data objpara_tab type table of selc.

data objparb_tab type table of soop1.

data sood_key type soodk.

data hex_mode type sonv-flag.

field-symbols <fs> type line of ty_ysood_tab.

loop at t_attachments assigning <fs>.

if not ( <fs>-objtp is initial or <fs>-objyr is initial or <fs>-objno is initial ).

concatenate <fs>-objtp <fs>-objyr <fs>-objno into sood_key.

perform socx_select in program sapfsso0

tables objhead_tab objcont_tab

objpara_tab objparb_tab

using sood_key

hex_mode

rcode.

if rcode eq 0.

data moff type i.

data l_param_search type soli-line.

data l_param_head type soli-line.

data value type soli-line.

data wa_objhead_tab like line of objhead_tab.

data lt_url_tab type table of so_url.

data ld_url_tab_size type sytabix.

l_param_search = '&SO_FILENAME'.

translate l_param_search to upper case.

loop at objhead_tab into wa_objhead_tab.

clear moff.

find '=' in wa_objhead_tab-line match offset moff.

check sy-subrc = 0.

l_param_head = wa_objhead_tab-line(moff).

translate l_param_head to upper case.

if l_param_head = l_param_search.

add 1 to moff.

value = wa_objhead_tab-line+moff.

if not ( value is initial ).

split value at '.' into table lt_url_tab.

describe table lt_url_tab lines ld_url_tab_size.

if ld_url_tab_size gt 1.

read table lt_url_tab index ld_url_tab_size into <fs>-acnam.

endif.

endif.

endif.

endloop.

endif.

endif.

endloop.

else.

es_return-type = 'E'.

es_return-text = 'There are no Attachments on this Business Object'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'No Business Object and/or Key Specified'.

endif.

endmethod.

method gos_attach_file.

data: ls_object type borident,

ls_obj_data type sood1,

ls_obj_id type soodk,

ls_fol_id type soodk,

ld_filename type string,

lt_url_tab type table of so_url,

ld_url_tab_size type sytabix,

ls_folmem_k type sofmk,

lv_ep_note type borident-objkey,

ls_note type borident.

data: it_objhead type standard table of soli.

ls_object-objtype = objtype.

ls_object-objkey = objkey.

if not ls_object-objtype is initial and not ls_object-objkey is initial.

call function 'SO_CONVERT_CONTENTS_BIN'

exporting

it_contents_bin = it_content[]

importing

et_contents_bin = it_content[].

if sy-subrc eq 0.

call function 'SO_FOLDER_ROOT_ID_GET'

exporting

region = folder_region

importing

folder_id = ls_fol_id

exceptions

others = 1.

if sy-subrc eq 0.

if not file_name_with_path is initial.

call function 'SO_SPLIT_FILE_AND_PATH'

exporting

full_name = file_name_with_path

importing

stripped_name = ld_filename.

else.

call function 'SO_SPLIT_FILE_AND_PATH'

exporting

full_name = file_name_no_path

importing

stripped_name = ld_filename.

endif.

if sy-subrc eq 0.

split ld_filename at '.' into table lt_url_tab.

describe table lt_url_tab lines ld_url_tab_size.

if ld_url_tab_size gt 1.

read table lt_url_tab index ld_url_tab_size into ls_obj_data-file_ext.

read table lt_url_tab index 1 into ls_obj_data-objdes.

else.

clear ls_obj_data-file_ext.

ls_obj_data-objdes = ld_filename.

endif.

ls_obj_data-objsns = 'O'.

ls_obj_data-objla = sy-langu.

ls_obj_data-objlen = lines( it_content ) * 255.

data: wa_content type soli.

clear wa_content.

concatenate '&SO_FILENAME=' ld_filename into wa_content.

append wa_content to it_objhead.

call function 'SO_OBJECT_INSERT'

exporting

folder_id = ls_fol_id

object_type = 'EXT'

object_hd_change = ls_obj_data

importing

object_id = ls_obj_id

tables

objhead = it_objhead

objcont = it_content

exceptions

active_user_not_exist = 35

folder_not_exist = 6

object_type_not_exist = 17

owner_not_exist = 22

parameter_error = 23

others = 1000.

if sy-subrc = 0 and ls_object-objkey is not initial.

commit work and wait.

ls_folmem_k-foltp = ls_fol_id-objtp.

ls_folmem_k-folyr = ls_fol_id-objyr.

ls_folmem_k-folno = ls_fol_id-objno.

ls_folmem_k-doctp = ls_obj_id-objtp.

ls_folmem_k-docyr = ls_obj_id-objyr.

ls_folmem_k-docno = ls_obj_id-objno.

lv_ep_note = ls_folmem_k.

ls_note-objtype = 'MESSAGE'.

ls_note-objkey = lv_ep_note.

call function 'BINARY_RELATION_CREATE_COMMIT'

exporting

obj_rolea = ls_object

obj_roleb = ls_note

relationtype = 'ATTA'

exceptions

others = 1.

if sy-subrc eq 0.

es_return-text = 'SUCCESS'.

commit work and wait.

else.

es_return-type = 'E'.

es_return-text = 'Error Calling: BINARY_RELATION_CREATE_COMMIT'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'Error Calling: SO_OBJECT_INSERT'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'Error With File Name'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'Error Obtaining Root Folder ID'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'Error Converting Table Contents to Binary'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'No Business Object and/or Key Specified'.

endif.

endmethod.

method gos_download_file.

data: ex type ref to cx_root, text type string.

data: ltp_sortfield type char30,

lta_objcont type soli_tab,

ltp_pathfile(1000) type c,

ltp_filename type string,

ltp_binfilesize type so_doc_len.

try .

concatenate attachment-objtp attachment-objyr attachment-objno into ltp_sortfield.

import objcont_tab to lta_objcont from database soc3(dt) id ltp_sortfield.

if sy-subrc = 0.

concatenate file_path '\' attachment-objdes '.' attachment-file_ext into ltp_pathfile.

replace '\\' with '\' into ltp_pathfile+2.

translate ltp_pathfile using '/ '.

ltp_binfilesize = attachment-objlen.

call function 'SO_OBJECT_DOWNLOAD'

exporting

bin_filesize = ltp_binfilesize

filetype = 'BIN'

path_and_file = ltp_pathfile

extct = attachment-extct

no_dialog = 'X'

importing

act_filename = ltp_filename

tables

objcont = lta_objcont

exceptions

file_write_error = 1

invalid_type = 2

x_error = 3

kpro_error = 4

others = 5.

if sy-subrc <> 0.

es_return-type = 'E'.

es_return-text = 'Error Calling: SO_OBJECT_DOWNLOAD'.

else.

es_return-type = 'S'.

es_return-text = 'SUCCESS'.

endif.

else.

es_return-type = 'E'.

es_return-text = 'Error With Object Import From DB'.

endif.

catch cx_root into ex.

es_return-text = ex->get_text( ).

endtry.

endmethod.

endclass.

*&---------------------------------------------------------------------*

*& Form UPLOAD

*&---------------------------------------------------------------------*

*& text

*&---------------------------------------------------------------------*

*& --> p1 text

*& <-- p2 text

*&---------------------------------------------------------------------*

form upload .

data: g_filename type string,

g_attsize type wsrm_error-wsrm_direction,

it_content type SOLI_TAB.

data: ta_srgbtbrel type standard table of srgbtbrel, wa_srgbtbrel type srgbtbrel,

lta_sood type standard table of sood, lwa_sood type sood.

data: t_st_return type lcl=>ty_yreturn.

data dec_kb type p.

* LIST

call method lcl=>gos_get_file_list

exporting

objtype = p_type

objkey = p_key

importing

t_attachments = lta_sood

changing

es_return = t_st_return.

if t_st_return-type = 'S'.

loop at lta_sood into lwa_sood.

dec_kb = lwa_sood-objlen / 1024.

if dec_kb < 1.

dec_kb = 1.

endif.

write: / lwa_sood-objdes, dec_kb, 'KB'.

endloop.

else.

message t_st_return-text type 'W'.

endif.

* ATTACH

move p_file to g_filename.

call function 'GUI_UPLOAD'

exporting

filename = g_filename

filetype = 'BIN'

importing

filelength = g_attsize

tables

data_tab = it_content

exceptions

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

others = 17.

if sy-subrc eq 0.

data: l_file_name_with_path type avwctxcont.

move g_filename to l_file_name_with_path.

call method lcl=>gos_attach_file

exporting

objtype = p_type

objkey = p_key

folder_region = 'B'

file_name_with_path = l_file_name_with_path

importing

es_return = t_st_return

CHANGING

it_content = it_content.

if t_st_return-type = 'S'.

message t_st_return-text type 'I'.

else.

message t_st_return-text type 'W'.

endif.

endif.

* LIST

call method lcl=>gos_get_file_list

exporting

objtype = p_type

objkey = p_key

importing

t_attachments = lta_sood

changing

es_return = t_st_return.

if t_st_return-type = 'S'.

write: /.

loop at lta_sood into lwa_sood.

dec_kb = lwa_sood-objlen / 1024.

if dec_kb < 1.

dec_kb = 1.

endif.

write: / lwa_sood-objdes, dec_kb, 'KB'.

endloop.

else.

message t_st_return-text type 'S' DISPLAY LIKE 'W'.

endif.

endform.

*&---------------------------------------------------------------------*

*& Form DOWNLOAD

*&---------------------------------------------------------------------*

*& text

*&---------------------------------------------------------------------*

*& --> p1 text

*& <-- p2 text

*&---------------------------------------------------------------------*

form download.

data: lta_sood type lcl=>ty_ysood_tab,

lwa_sood type sood.

data: t_st_return type lcl=>ty_yreturn.

call method lcl=>gos_get_file_list

exporting

objtype = p_type

objkey = p_key

importing

t_attachments = lta_sood

changing

es_return = t_st_return.

if t_st_return-type = 'S'.

data dec_kb type p.

loop at lta_sood into lwa_sood.

call method lcl=>gos_download_file

exporting

file_path = 'C:\TEMP'

attachment = lwa_sood

changing

es_return = t_st_return.

if t_st_return-type = 'S'.

dec_kb = lwa_sood-objlen / 1024.

if dec_kb < 1.

dec_kb = 1.

endif.

write: / lwa_sood-objdes, dec_kb, 'KB'.

else.

message t_st_return-text type 'W'.

endif.

endloop.

else.

message t_st_return-text type 'S' DISPLAY LIKE 'W'.

endif.

endform.

*&---------------------------------------------------------------------*

*& Form LIST

*&---------------------------------------------------------------------*

*& text

*&---------------------------------------------------------------------*

*& --> p1 text

*& <-- p2 text

*&---------------------------------------------------------------------*

form list.

data: ta_srgbtbrel type standard table of srgbtbrel, wa_srgbtbrel type srgbtbrel,

lta_sood type standard table of sood, lwa_sood type sood.

data: t_st_return type lcl=>ty_yreturn.

call method lcl=>gos_get_file_list

exporting

objtype = p_type

objkey = p_key

importing

t_attachments = lta_sood

changing

es_return = t_st_return.

if t_st_return-type = 'S'.

data dec_kb type p.

loop at lta_sood into lwa_sood.

dec_kb = lwa_sood-objlen / 1024.

if dec_kb < 1.

dec_kb = 1.

endif.

write: / lwa_sood-objdes, dec_kb, 'KB'.

endloop.

else.

message t_st_return-text type 'S' display like 'W'.

endif.

endform.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 *&---------------------------------------------------------------------* *& Report  ZATTACHMENT_MASS_EXT *&---------------------------------------------------------------------* *& Mass extraction of SAP Attachment for Sales Document *& Files will be download to local directory *&---------------------------------------------------------------------*   REPORT  Zattachment_mass_ext. TABLES: vbak. TYPES: BEGIN OF ty_vbeln,           vbeln    TYPE vbeln,           instid_b LIKE srgbtbrel-instid_b,        END OF ty_vbeln.   DATA: BEGIN OF sood_tab OCCURS 0,         objtp  LIKE sood-objtp,         objyr  LIKE sood-objyr,         objno  LIKE sood-objno,         objdes LIKE sood-objdes,         file_ext LIKE sood-file_ext,         objlen LIKE sood-objlen,         extct  LIKE sood-extct,                                   END OF sood_tab.   DATA: lt_vbeln TYPE TABLE OF ty_vbeln. DATA: bin_filesize LIKE soxwd-doc_length. DATA: bakfilename_g LIKE rlgrap-filename. DATA: soc3key_g     LIKE soc3-srtfd. DATA: lv_file TYPE string. DATA: lv_vbeln TYPE string. DATA objcont_tab LIKE soli  OCCURS 0 WITH HEADER LINE. DATA: l_result         TYPE c,       l_datfilename    TYPE string,       rtcode_g         LIKE sy-subrc.   FIELD-SYMBOLS:  LIKE LINE OF sood_tab. FIELD-SYMBOLS:  LIKE LINE OF lt_vbeln.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 SELECT-OPTIONS: s_erdat FOR vbak-erdat. SELECT-OPTIONS: s_vbeln FOR vbak-vbeln. SELECT-OPTIONS: s_ktaar FOR vbak-ktaar. SELECT-OPTIONS: s_vkorg FOR vbak-vkorg.   PARAMETERS: p_lpath LIKE rlgrap-filename DEFAULT 'C:'.   " Help Search for Local Directory search AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_lpath.   DATA : lv_folder TYPE string.     CALL METHOD cl_gui_frontend_services=>directory_browse     EXPORTING       window_title    = 'Please Select a destination folder'       initial_folder  = 'C:'     CHANGING       selected_folder = lv_folder.     p_lpath = lv_folder .   CALL METHOD cl_gui_cfw=>flush.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 START-OF-SELECTION.     " Retrieve list of SAP Attachment   SELECT vbak~vbeln srgbtbrel~instid_b FROM srgbtbrel AS srgbtbrel   INNER JOIN vbak AS vbak   ON vbak~vbeln = srgbtbrel~instid_a   APPENDING TABLE lt_vbeln   PACKAGE SIZE 5000   WHERE  vbak~vbeln IN  s_vbeln[]     AND  vbak~erdat IN s_erdat[]     AND  vbak~ktaar IN s_ktaar[]     AND  vbak~vkorg IN s_vkorg[]     AND  vbak~kunnr IN s_kunnr[]     AND ( vbak~trvog   = 'A' OR vbak~trvog = '9' )     AND  srgbtbrel~reltype = 'ATTA'     AND  ( srgbtbrel~typeid_a = 'BUSXXX' OR srgbtbrel~typeid_a = 'BUSXXX' )     AND  srgbtbrel~typeid_b = 'MESSAGE'.   ENDSELECT.     CHECK  lt_vbeln[] IS NOT INITIAL.   SORT lt_vbeln BY instid_b.   DELETE ADJACENT DUPLICATES FROM lt_vbeln COMPARING instid_b.     " Retrieve File Attributs of SAP Attachments   SELECT objtp objyr objno objdes file_ext objlen extct     APPENDING TABLE sood_tab FROM sood     PACKAGE SIZE 5000     FOR ALL ENTRIES IN lt_vbeln     WHERE objtp = lt_vbeln-instid_b+17(3)     AND   objyr = lt_vbeln-instid_b+20(2)     AND   objno = lt_vbeln-instid_b+22(12).   ENDSELECT.     CHECK sood_tab[] IS NOT INITIAL.   SORT sood_tab BY objtp objyr objno.   DELETE ADJACENT DUPLICATES FROM sood_tab COMPARING objtp objyr objno.     LOOP AT sood_tab ASSIGNING  WHERE objlen GT 0.     REFRESH objcont_tab[].       CLEAR:  lv_vbeln, l_result , l_datfilename, lv_file,             bin_filesize, bakfilename_g, lv_file, soc3key_g.       " Check VBELN     LOOP AT lt_vbeln ASSIGNING         WHERE instid_b+17(3)  = -objtp         AND   instid_b+20(2)  = -objyr         AND   instid_b+22(12) = -objno.         lv_vbeln = -vbeln.       EXIT.     ENDLOOP.       IF lv_vbeln IS INITIAL .       CONTINUE.     ENDIF.       " Build File Name for SAP Attachment     CONCATENATE lv_vbeln '_' -objdes '.' -file_ext INTO lv_file.       CONDENSE lv_file NO-GAPS.       " Replace unsupported caracters in windows filename     REPLACE ALL OCCURRENCES OF '/' IN lv_file WITH '_'. " for example       IF p_local = 'X'.       CONCATENATE p_lpath '' lv_file INTO lv_file.     ENDIF.     REPLACE ALL OCCURRENCES OF '' IN lv_file WITH ''.       " Check if file exist     l_datfilename = lv_file.       CALL METHOD cl_gui_frontend_services=>file_exist       EXPORTING         file                 = l_datfilename       RECEIVING         result               = l_result       EXCEPTIONS         cntl_error           = 1         error_no_gui         = 2         wrong_parameter      = 3         not_supported_by_gui = 4         OTHERS               = 5.     IF sy-subrc <> 0.       CONTINUE.     ENDIF.     IF l_result IS NOT INITIAL.       APPEND INITIAL LINE TO lt_return ASSIGNING .       -type        = 'I'.       -number      = 'XXX'.       -id          = 'ZMSG'.       -message_v1  = lv_file.       CONCATENATE lv_file 'already exists' INTO -message SEPARATED BY space.       CONTINUE.     ENDIF.          " Build SAP Attachment Key     CONCATENATE -objtp -objyr -objno             INTO soc3key_g.       IMPORT objcont_tab FROM DATABASE soc3(dt) ID soc3key_g.     IF sy-subrc EQ 0.         bin_filesize = -objlen.       bakfilename_g = lv_file.         " Download SAP Attachment       CALL FUNCTION 'SO_OBJECT_DOWNLOAD'         EXPORTING           bin_filesize     = bin_filesize           filetype         = 'BIN'           path_and_file    = bakfilename_g           extct            = -extct           no_dialog        = 'X'         TABLES           objcont          = objcont_tab         EXCEPTIONS           file_write_error = 1           invalid_type     = 2           x_error          = 3           kpro_error       = 4           OTHERS           = 5.       IF sy-subrc = 0.         APPEND INITIAL LINE TO lt_return ASSIGNING .         -type        = 'S'.         -number      = '000'.         -id          = 'ZXXX'.         -message_v1  = lv_file.         CONCATENATE  lv_file 'SAP Attachment cannot be extracted'  INTO -message SEPARATED BY space.         CONTINUE.       ELSE.         APPEND INITIAL LINE TO lt_return ASSIGNING .         CALL FUNCTION 'BALW_BAPIRETURN_GET2'           EXPORTING             type   = sy-msgty             cl     = sy-msgid             number = sy-msgno             par1   = sy-msgv1             par2   = sy-msgv2             par3   = sy-msgv3             par4   = sy-msgv4           IMPORTING             return = .         CONTINUE.       ENDIF.     ENDIF.       REFRESH objcont_tab[].   ENDLOOP.     REFRESH objcont_tab[].   REFRESH lt_vbeln[].
返回ABAP参考资料列表