You are on page 1of 15

Requirement : Uploading and Downloading of an Image in Sap

Using ODATA.

Step-1 : Create the Data Base Table with following fields

Step-2 : Create the model structure with following fields


Step-3 :Navigate to Segw and create the project as shown below
Step-4 : Import the DDic Structure to Create the entity as shown
below
Step-5 : Create the entity Set as shown Below

Step-6 : Click the check box for Media as shown below to make
the entity as Media Resource
Step-7 : Generate the Project as shown below

Step-8 : Need to SPECIFY which of the Entity type properties


stores the MIME TYPE. This is done through the REDEFINITION
of the method 'DEFINE' of the CLASS MPC_EXT.
Code to be written in the method define is as follows
data : lo_entity_type type ref to /iwbep/if_mgw_odata_entity_typ,
lo_property type ref to /iwbep/if_mgw_odata_property.

"Call the super-class method FIRST!!!

super->define( ).

"Retrieve the entity type and store it in lo_entity_type

lo_entity_type = model->get_entity_type('Image').
lo_entity_type->set_is_media( ).

IF lo_entity_type is bound .
"Retrieve the corresponding entity type property
"and store it in lo_property.

lo_property = lo_entity_type->get_property( 'Mimetype' ).

"Now set lo_property as MIME TYPE


"using the method 'set_as_content_type( )'
lo_property->set_as_content_type( ).

ENDIF.
Step-9 : Need to Redefine the Create Stream method in the Dpc
extn Class

This method actually performs the File UPLOAD process. The file
which is inserted in the HTTP POST request
" has to be saved in the transparent table, predefined for it. The
table name is ' zimg_upd'. And redifne the get stream and get
entity methods
Code implementation of create stream
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM.

data: ls_file type zimg_upd,


ls_key_tab type /iwbep/s_mgw_name_value_pair,
ls_entity TYPE ZCL_ZIMAGEUPD_MPC=>TS_IMAGE,
lt_key_tab type /iwbep/t_mgw_name_value_pair.
"Now fill the ls_file accordingly

ls_file-filename = iv_slug.
ls_file-createdon = sy-datum.
ls_file-createdby = sy-uname.
ls_file-createdat = sy-uzeit.
ls_file-mimetype = is_media_resource-mime_type.
ls_file-value = is_media_resource-value.

"The idea is to fill the lt_key_tab


"with filename and mimetype using ls_key_tab

ls_key_tab-name = 'FileName'.
ls_key_tab-value = iv_slug.
append ls_key_tab to lt_key_tab.

ls_key_tab-name = 'MIMEType'.
ls_key_tab-value = is_media_resource-mime_type.
append ls_key_tab to lt_key_tab.

"Now add the file to the transparent table

modify zimg_upd from ls_file.

if sy-subrc = 0.
"Now call the FILESET_GET_ENTITY method
images_get_entity(
exporting
iv_entity_name = iv_entity_name
iv_entity_set_name = iv_entity_set_name
iv_source_name = iv_source_name
it_key_tab = lt_key_tab
it_navigation_path = it_navigation_path
importing
er_entity = ls_entity
) .
"Fill the export parameter er_entity accordingly
copy_data_to_ref(
exporting
is_data = ls_entity
changing
cr_data = er_entity ).
endif.
endmethod.

Code implementation of image get entity


method IMAGES_GET_ENTITY.

data : lv_filename type string,


lv_mimetype type string.

field-symbols <lf_keys> type /iwbep/s_mgw_name_value_pair.

data ls_file type zimg_upd.

"Read the the parameters from the it_key_tab


read table it_key_tab assigning <lf_keys> with key name = 'FileName'.
if sy-subrc = 0.
lv_filename = <lf_keys>-value.
endif.

read table it_key_tab assigning <lf_keys> with key name = 'MIMEType'.


if sy-subrc = 0.
lv_mimetype = <lf_keys>-value.
endif.

"First be assured that lv_filename and lv_mimetype are NOT initial

if lv_filename is not initial and lv_mimetype is not initial .

"Now read read the entry from the database table


select single * from zimg_upd into ls_file
where filename = lv_filename and mimetype = lv_mimetype .
if sy-subrc = 0.
"The entry was FOUND, now you can fill the er_entity accordingly
er_entity-filename = ls_file-filename.
er_entity-mimetype = ls_file-mimetype.
er_entity-value = ls_file-value.
er_entity-createdon = ls_file-createdon.
er_entity-createdby = ls_file-createdby.
er_entity-createdat = ls_file-createdat.
endif.
endif.
endmethod.

Code implementation of Get Stream.


method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM.

data: ls_stream type ty_s_media_resource,


ls_upld type zimg_upd,
lv_filename type char30.

field-symbols <fs_key> like line of it_key_tab.

read table it_key_tab assigning <fs_key> index 1.


if sy-subrc eq 0.
lv_filename = <fs_key>-value.
endif.
"Now select the corresponding file from the
"database to perform the DOWNLOAD
select single * from zimg_upd into
ls_upld where filename = lv_filename.
if ls_upld is not initial.
"Now copy the binary data
ls_stream-value = ls_upld-value.
ls_stream-mime_type = ls_upld-mimetype.

copy_data_to_ref(

exporting
is_data = ls_stream
changing
cr_data = er_stream

) .

endif.
endmethod.
Step-10 : Generating the Service as shown below

Click on the Add Service


Step-11 : Uploading the Image
While uploading the image we need to add the following
parameters in the header

Second parameter is we need to mention image name


Click on Execute button as shown in the below image
url : /sap/opu/odata/sap/ZIMAGEUPD_SRV/Images

Image was posted successfully as shown in the below screenshot


Check in the table whether the image has been stored or not

Step-12 : To Retrieve the image as shown below


url:
/sap/opu/odata/sap/ZIMAGEUPD_SRV/Images('elephant1.jpg')/$
value
Click on execute to retrieve the image as shown below

You might also like