You might have a task to use or download one or all files attached to document saved via CV01N transaction. This is not a big deal really, you just need to use some FMs available in standard of SAP like 'CVAPI_DOC_GETDETAIL', 'CVAPI_DOC_CHECKOUTVIEW' or SCMS_DOC_READ'. These FMs allows you the check the details of document, check it and then read its content. Of course sometimes you'll have to zip the files before the downloading and here in my code I used method "zip" for which code you can find here. While reading the code you may also find another Z-method called "get_filename_from_path", its code is posted here. The method I'm posting bellow allows you to choose if during download of files from document you want to zip them all together into one zip file or if you want to zip them separatelly. In table ET_FILES you'll find all files that were attached to document, field "UPDATEFLAG" will be set to 'D' if file was succesfully downloaded to PC. If you don't want to download the file but you want to get it for futher use you'll have to request ET_XSTRING table. In other case do not request this parameter as it is a sign for method that you don't want to donwload the files.
Types:
TT_RA_FILENAME type range of CVAPI_DOC_FILE-FILENAME.
TT_XSTRING type table of xstring.
Importing:
I_DOKAR TYPE DRAW-DOKAR ->Document Type
I_DOKNR TYPE DRAW-DOKNR ->Document number
I_DOKTL TYPE DRAW-DOKTL ->Document Part
I_DOKVR TYPE DRAW-DOKVR ->Document Version
I_DOWNLOAD_DIRECTORY TYPE STRING -> path to download the fiels
I_ZIP_FILES TYPE C OPTIONAL -> mark it 'X' if you want to zip the files before downloading
I_ZIP_SEPARATELY TYPE C OPTIONAL -> mark it 'X' if you want to zip each file found in document separately
I_NAME_FILTER TYPE TT_RA_FILENAME OPTIONAL -> Range for filter filenames - select which documents you want to download
I_FILENAME TYPE CSEQUENCE OPTIONAL -> filename for zip - used if you zip all files into zip container
Exporting:
ET_FILES TYPE CVAPI_TBL_DOC_FILES -> Table with all files found in document
ET_XSTRING TYPE TT_XSTRING -> TT_XSTRING -> Table of xstrings (each file in separated line)
Exceptions:
ZIP_ERROR -> Error while zipping
DOCUMENT_ERROR -> Error while reading document
Implementation:
method save_document_on_pc.
data: m_exists type abap_bool.
data: m_temp_dir(255) type c.
data: mt_drat type table of drat.
data: ms_drat type drat.
data: mt_files type table of cvapi_doc_file.
data: mt_components type table of cvapi_doc_comp.
data: ms_draw type draw.
data: m_mimetype type mimetypes-type.
data: mt_files_checkout type table of cvapi_doc_file.
data: ms_files_checkout type cvapi_doc_file.
data: mt_content type table of drao.
data: ms_content type drao.
data: ms_message type messages.
data: m_fail type c.
data: m_size type i.
data: ms_bindata type xstring.
data: ms_zipdata type xstring.
data: m_filename type string.
data: m_filenamefull type string.
data: m_extension type string.
data: mo_zipper type ref to cl_abap_zip.
data: mt_accessinf type standard table of scms_acinf.
data: ms_accessinf type scms_acinf.
data: mr_name_filter type tt_ra_filename.
field-symbols <filter> like line of mr_name_filter.
data: m_fileupper type string.
mr_name_filter[] = i_name_filter[].
loop at mr_name_filter assigning <filter>.
translate <filter>-low to upper case.
translate <filter>-high to upper case.
endloop.
types: begin of t_binline,
line type drao-orblk,
end of t_binline.
data: mt_bintab type standard table of sdokcntbin.
data: ms_bintab type sdokcntbin.
field-symbols: <files> type cvapi_doc_file.
create object mo_zipper.
ms_draw-dokar = i_dokar.
ms_draw-doknr = i_doknr.
ms_draw-doktl = i_doktl.
ms_draw-dokvr = i_dokvr.
call function 'CVAPI_DOC_GETDETAIL'
exporting
pf_dokar = ms_draw-dokar
pf_doknr = ms_draw-doknr
pf_dokvr = ms_draw-dokvr
pf_doktl = ms_draw-doktl
pf_read_drat = abap_true
pf_read_comp = abap_true
importing
psx_draw = ms_draw
tables
pt_drat = mt_drat
pt_files = mt_files
pt_comp = mt_components
exceptions
not_found = 1
no_auth = 2
error = 3
others = 4.
if sy-subrc ne 0.
raise document_error.
else.
loop at mt_files assigning <files>.
m_fileupper = <files>-filename.
translate m_fileupper to upper case.
if m_fileupper not in mr_name_filter.
append <files> to et_files.
continue.
endif.
clear mt_files_checkout.
clear mt_content.
ms_files_checkout-description = <files>-description.
ms_files_checkout-lo_objid = <files>-lo_objid.
ms_files_checkout-ph_objid = <files>-ph_objid.
append ms_files_checkout to mt_files_checkout.
call function 'CVAPI_DOC_CHECKOUTVIEW'
exporting
pf_dokar = ms_draw-dokar
pf_doknr = ms_draw-doknr
pf_dokvr = ms_draw-dokvr
pf_doktl = ms_draw-doktl
* pf_content_provide = 'TBL'
importing
psx_message = ms_message
psx_draw = ms_draw
tables
pt_files = mt_files_checkout
* ptx_content = mt_content
.
if ms_message-msg_type ca 'EA'.
m_fail = abap_true.
<files>-updateflag = 'E'. "error
append <files> to et_files.
continue.
else.
clear mt_bintab.
clear ms_bindata.
refresh: mt_bintab, mt_accessinf.
call function 'SCMS_DOC_READ'
exporting
stor_cat = <files>-storage_cat
doc_id = <files>-ph_objid
tables
access_info = mt_accessinf
content_bin = mt_bintab
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 eq 0.
read table mt_accessinf into ms_accessinf index 1.
m_size = ms_accessinf-comp_size.
if sy-subrc eq 0.
z_my_special_class=>get_filename_from_path(
exporting
i_path = <files>-filename
importing
e_filename = m_filename
e_filenamefull = m_filenamefull
e_extension = m_extension
).
if i_zip_files is not initial or et_xstring is requested.
call function 'SCMS_BINARY_TO_XSTRING'
exporting
input_length = ms_accessinf-comp_size
importing
buffer = ms_bindata
tables
binary_tab = mt_bintab
exceptions
failed = 1
others = 2.
if sy-subrc eq 0.
if et_xstring is requested.
append ms_bindata to et_xstring.
<files>-updateflag = 'D'.
append <files> to et_files.
continue.
endif.
"just for zipping
if i_zip_separately is not initial.
z_my_special_class=>zip(
exporting
i_xstring = ms_bindata
* i_string = i_string
i_filename = m_filenamefull
* i_solix = i_solix
* i_soli = i_soli
i_get_xstring = 'X'
importing
e_xstring = ms_zipdata
exceptions
attachment_is_empty = 1
string_conv_error = 2
others = 3
).
if sy-subrc <> 0.
raise zip_error.
endif.
else.
z_my_special_class=>zip(
exporting
i_xstring = ms_bindata
* i_string = i_string
i_filename = m_filenamefull
* i_solix = i_solix
* i_soli = i_soli
i_get_xstring = space
changing
c_zipper = mo_zipper
* importing
* e_xstring = ms_zipdata
exceptions
attachment_is_empty = 1
string_conv_error = 2
others = 3
).
if sy-subrc <> 0.
raise zip_error.
endif.
endif.
if i_zip_separately is not initial.
ms_bindata = ms_zipdata.
m_extension = 'zip'.
refresh mt_bintab[].
* Convert the XSTRING to Binary table
call function 'SCMS_XSTRING_TO_BINARY'
exporting
buffer = ms_bindata
tables
binary_tab = mt_bintab.
endif.
endif.
endif.
if i_zip_separately is not initial or i_zip_files is initial.
clear m_exists.
cl_gui_frontend_services=>directory_exist(
exporting
directory = i_download_directory
receiving
result = m_exists
exceptions
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
others = 5
).
if sy-subrc eq 0 and m_exists eq 'X' .
concatenate i_download_directory '\' m_filenamefull into m_filename.
* Download the Zip file to temp directory
cl_gui_frontend_services=>gui_download(
exporting
bin_filesize = m_size
filename = m_filename
filetype = 'BIN'
write_bom = 'X'
changing
data_tab = mt_bintab
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
not_supported_by_gui = 22
error_no_gui = 23
others = 24
).
if sy-subrc eq 0.
<files>-updateflag = 'D'.
append <files> to et_files.
continue.
else.
append <files> to et_files.
endif.
endif.
endif.
endif.
else.
append <files> to et_files.
continue.
endif.
endif.
endloop.
if i_zip_separately is initial and i_zip_files is not initial.
refresh mt_bintab[].
clear ms_bindata.
ms_bindata = mo_zipper->save( ).
check ms_bindata is not initial.
clear ms_drat.
read table mt_drat with key langu = sy-langu into ms_drat.
if sy-subrc ne 0.
read table mt_drat with key langu = 'E' into ms_drat.
if sy-subrc ne 0.
read table mt_drat index 1 into ms_drat.
endif.
endif.
if ms_drat-dktxt is initial.
if i_filename is not initial.
m_filenamefull = i_filename.
else.
concatenate sy-datum sy-uzeit into m_filenamefull.
endif.
endif.
* Convert the XSTRING to Binary table
call function 'SCMS_XSTRING_TO_BINARY'
exporting
buffer = ms_bindata
tables
binary_tab = mt_bintab.
clear m_exists.
cl_gui_frontend_services=>directory_exist(
exporting
directory = i_download_directory
receiving
result = m_exists
exceptions
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
others = 5
).
if sy-subrc eq 0 and m_exists eq 'X' .
concatenate i_download_directory '\' m_filename '.zip' into m_filename.
* Download the Zip file to temp directory
cl_gui_frontend_services=>gui_download(
exporting
bin_filesize = m_size
filename = m_filename
filetype = 'BIN'
write_bom = 'X'
changing
data_tab = mt_bintab
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
not_supported_by_gui = 22
error_no_gui = 23
others = 24
).
if sy-subrc eq 0.
endif.
endif.
endif.
endif.
endmethod.
Enjoy!