Vous êtes sur la page 1sur 2

How to store file incoming as XSTRING

in SAP
June 2, 2014December 16, 2014 File upload & download, SAP Netweaver gateway

Lets assume that we are redefining method CREATE_STREAM of our extended Data Provider
Class. We are importing structure which contains mime type and content of the file as XSTRING.
This structure is IS_MEDIA_RESOURCE with type TY_S_MEDIA_RESOURCE.
Before we start, if you want to know what is XSTRING, you can take a look at my other article.

First option Storing XSTRING in SAP Office


Proposed solution is to save the file into SAP office using function module
SO_DOCUMENT_INSERT_API1 from a function group providing SAP Office API SOI1.
1. Converting XSTRING to SOLIX
Best way to supply content of the file is in SOLIX structure which is a table of hexadecimal
characters. We can use the following approach to solve this issue.
DATA l_file TYPE solix_tab.
lt_file = cl_bcs_convert=>xstrin

1
2

DATA l_file TYPE solix_tab.


lt_file = cl_bcs_convert=>xstring_to_solix( iv_xstring = iv_content ).

2. Using SO_DOCUMENT_INSERT_API1 to store file in SAP Office


Now, we can save the content of a file in hexadecimal characters to SAP Office using FM API. We
need to provide at least these parameters:

folder_id ID of the folder where we want to store data

document_data Filled document data

document_type Extension of the file

contents_hex Content of the file in hexadecimal table

object_header Header with filename

CALL FUNCTION 'SO_DOCUMEN


EXPORTING
folder_id
= lv_folde
document_data
= ls_
document_type
= lv_

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'


EXPORTING
folder_id
= lv_folder_id
document_data
= ls_doc_data
document_type
= lv_type
IMPORTING
document_info
= ls_doc_info
TABLES
contents_hex
= lt_file
object_header
= lt_header
EXCEPTIONS
folder_not_exist
=1
document_type_not_exist = 2
operation_no_authorization = 3
parameter_error
=4
x_error
=5
enqueue_error
=6
OTHERS
= 7.

With successful implementation, file can be transferred from a web client through an OData
service into SAP office.

Second option Storing XSTRING in database


There is also an option to store XSTRING in database. It would be inefficient to store whole file
in database, thus only pointer is stored, content of the file is stored in storage. Performance
should not be affected, though it is recommended to store only small files in database. New
column with corresponding type RAWSTRING has to be created and then you can insert new file
into database.
ls_record-filename = iv_filename
ls_record-content = iv_content.
INSERT INTO files VALUES ls_re

1
2
3

ls_record-filename = iv_filename.
ls_record-content = iv_content.
INSERT INTO files VALUES ls_record.