Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Code to upload documents not working for .XLSX and .MSG extension

former_member6134
Participant
0 Kudos

I am uploading documents using OData through SAPUI5 application where uploaded .XLSX & .MSG extension are not opening correctly when downloaded it. Below given codes are used

For upload documents code written in services /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM


DATA ls_folder_id TYPE soodk.
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
EXPORTING
region = 'B'
IMPORTING
folder_id = ls_folder_id
EXCEPTIONS
communication_failure = 1
owner_not_exist = 2
system_failure = 3
x_error = 4
OTHERS = 5.

DATA lv_extension TYPE sood-file_ext.
CALL FUNCTION 'TRINT_FILE_GET_EXTENSION'
EXPORTING
filename = iv_filename
IMPORTING
extension = lv_extension.
* to convert the file content to binary
DATA: lv_filesize TYPE i,
lv_base64 TYPE string,
lv_string TYPE string,
lv_binary TYPE xstring.

CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
input = iv_value "is_media_resource-value
IMPORTING
output = lv_base64.
* *Decode Base64 String to String
CALL METHOD cl_http_utility=>if_http_utility~decode_base64
EXPORTING
encoded = lv_base64
RECEIVING
decoded = lv_string.

CALL FUNCTION 'SSFC_BASE64_DECODE'
EXPORTING
b64data = lv_string
IMPORTING
bindata = lv_binary
EXCEPTIONS
ssf_krn_error = 1
ssf_krn_noop = 2
ssf_krn_nomemory = 3
ssf_krn_opinv = 4
ssf_krn_input_data_error = 5
ssf_krn_invalid_par = 6
ssf_krn_invalid_parlen = 7
OTHERS = 8.

DATA: lv_output_length TYPE i,
lt_solix TYPE solix_tab.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_binary
append_to_table = 'X'
IMPORTING
output_length = lv_output_length
TABLES
binary_tab = lt_solix.

DATA lt_soli TYPE soli_tab.
CALL FUNCTION 'SO_SOLIXTAB_TO_SOLITAB'
EXPORTING
ip_solixtab = lt_solix
IMPORTING
ep_solitab = lt_soli.

DATA: ls_object_hd_change TYPE sood1,
lt_objhead TYPE STANDARD TABLE OF soli.

ls_object_hd_change-objla = sy-langu.
ls_object_hd_change-objsns = 'O'.
* ls_object_hd_change-objnam = 'MESSAGE'.
ls_object_hd_change-objdes = iv_filename.
ls_object_hd_change-file_ext = lv_extension.
ls_object_hd_change-objlen = lv_output_length.

DATA lv_mimetype TYPE mimetypes-type.
CALL FUNCTION 'SDOK_MIMETYPE_GET'
EXPORTING
extension = lv_extension
IMPORTING
mimetype = lv_mimetype.

lt_objhead = VALUE #( ( line = '&SO_FILENAME=' && iv_filename )
( line = '&SO_FORMAT=BIN' )
( line = '&SO_CONTTYPE=' && lv_mimetype ) ).

DATA ls_object_id TYPE soodk.
CALL FUNCTION 'SO_OBJECT_INSERT'
EXPORTING
folder_id = ls_folder_id
object_hd_change = ls_object_hd_change
object_type = 'EXT'
IMPORTING
object_id = ls_object_id
TABLES
objcont = lt_soli
objhead = lt_objhead
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.
es_return-type = 'E'.
es_return-id = sy-msgid.
es_return-number = sy-msgno.
es_return-message_v1 = sy-msgv1.
es_return-message_v2 = sy-msgv2.
es_return-message_v3 = sy-msgv3.
es_return-message_v4 = sy-msgv4.
IF es_return-id IS INITIAL.
es_return-id = 'SV'.
es_return-number = '171'.
ENDIF.
RETURN.
ENDIF.

es_attachment-object_type = 'BUS2105'.
es_attachment-object_cat = 'BO'.
es_attachment-document_id = ls_folder_id-objtp && ls_folder_id-objyr && ls_folder_id-objno &&
ls_object_id-objtp && ls_object_id-objyr && ls_object_id-objno.
es_attachment-file_name = iv_filename.
es_attachment-mime_type = lv_mimetype.


DATA: ls_object_a TYPE sibflporb,
ls_object_b TYPE sibflporb.

ls_object_a-instid = iv_object_id.
ls_object_a-typeid = 'BUS2105'.
ls_object_a-catid = 'BO'.
ls_object_b-instid = es_attachment-document_id.
ls_object_b-typeid = 'MESSAGE'.
ls_object_b-catid = 'BO'.

TRY.
cl_binary_relation=>create_link(
EXPORTING
is_object_a = ls_object_a
is_object_b = ls_object_b
ip_reltype = 'ATTA' ).
CATCH cx_obl_parameter_error.
CATCH cx_obl_model_error.
CATCH cx_obl_internal_error.

ENDTRY.

2 REPLIES 2

mkabel
Participant
0 Kudos

Did you customize these mimetypes?

Have a look on note 1739026 - Attachment can not be displayed due to MIME-Type problems in ArchiveLink - SAP ONE Support...

hth

kind regards

Matthias

Sandra_Rossi
Active Contributor
0 Kudos

What kind of data do you receive in IV_VALUE? I don't understand your logic about encode/decode/decode base64 (why repeating?)