Store, read and download file from MIME repository

MIME repository is a nice and convenient way to store and provide built in documents for end users. It comes handy, when working with templates. User can download the up to date templates always. This is especially good, when users use excel documents, to load data into the system. In case the data model and the program to load the data changes, the user can download the corresponding template with the right data structure. Other use case is when you generate office documents in your program directly using XSLT, where you pickup the empty template from MIME repository first. Of course your possibilities are not restricted only to Office documents, MIME documents are variety of kind. The next example shows you, how-to download a file stored in SE80/MIME Repository (or using transaction SO2_MIME_REPOSITORY). Important thing to keep in mind, you need to deal with authorizations in case the file is stored not under the SAP/PUBLIC/ node in the MIME respository. The authorization check can be ignored in your ABAP program with parameter i_check_authority = abap_false of method get. Read more about MIME.

CONSTANTS:
co_mime_path  TYPE string VALUE 'SAP/PUBLIC/MY_FOLDER/My_File.xlsx'.
DATA:
rc TYPE i,
byte_count TYPE i,
file_table TYPE solix_tab,
filename TYPE string,
path TYPE string,
fullpath TYPE string.


cl_mime_repository_api=>get_api( )->get(
EXPORTING
i_url = co_mime_path "your string constant holding the MIME repostory path
IMPORTING
e_content = DATA(file)
EXCEPTIONS
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
OTHERS = 5
).

"Convert to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = file
IMPORTING
output_length = byte_count
TABLES
binary_tab = file_table.

"Let user browse path for download
cl_gui_frontend_services=>file_save_dialog(
EXPORTING
default_file_name = 'My_File.xlsx'
CHANGING
filename = filename
path = path
fullpath = fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
invalid_default_file_name = 4
OTHERS = 5
).
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

"Transfer to client
IF fullpath IS NOT INITIAL.

cl_gui_frontend_services=>gui_download(
EXPORTING
bin_filesize = byte_count
filename = fullpath
filetype = 'BIN'
CHANGING
data_tab = file_table
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 <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.