Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
nitinksh1
Participant

Introduction


In this blog, I would like to show you how to download a ZIP package stored on an application server via Odata call in very simple steps.

I have tried not to add any additional logic apart from downloading the ZIP package from the Odata service.

Steps


1. Table creation.

2. Create an oData service.

3. Redefine methods of DPC extension class GET_ENTITYSET, /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM.

4. Redefine methods of MPC extension class DEFINE.

5. Register the oData service.

6. Test

Implementation


Let's begin the development step by step.

Step 1


1. Create a table where we will have the package name stored.


Table




Step 2


2.1. Create an Odata service.


Odata service


 

2.2. Create an entity with an entity set.


Entity


 

2.3. Add table name to ABAP structure and also mark Media checkbox as true. Do remember to mark the Media as true.


Add Structure


 

2.4. Import Properties/fields from the custom table.


Import Properties


 

2.5. Now it's time to generate the service.


Generate Service


 

Step 3


3.1. Go to the DPC extension class, redefine the GET_ENTITYSET method and add logic to get data from the custom table. This is required so that we can fetch data from the table to get the package name.


DPC_EXT Fetch Data


3.2. In the DPC extension, redefine method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM and add the logic to get filename, convert it to xstring and pass it back to Odata.
 METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.

TYPES:
"! <p class="shorttext synchronized" lang="en">Media Type Information</p>
BEGIN OF ty_s_media_resource.
INCLUDE TYPE /iwbep/if_mgw_core_srv_runtime=>ty_s_media_resource.
TYPES:END OF ty_s_media_resource .

TYPES: BEGIN OF ty_package,
docnr TYPE zzdocnr,
filename TYPE zzfilename,
END OF ty_package.

DATA:ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
ls_package TYPE ty_package,
ls_stream TYPE ty_s_media_resource,
lv_docnr TYPE zzdocnr,
lo_meco TYPE REF TO /iwbep/if_message_container,
ls_lheader TYPE ihttpnvp,
lv_xstring TYPE xstring.

READ TABLE it_key_tab INTO ls_key_tab INDEX 1.
IF sy-subrc EQ 0.
lv_docnr = ls_key_tab-value.
ENDIF.

SELECT SINGLE docnr,
filename
FROM zzip_package INTO @ls_package
WHERE docnr = @lv_docnr.


DATA(lv_path) = '.\SamplePackage.zip'.
OPEN DATASET lv_path FOR INPUT IN BINARY MODE. "#EC CI_USE_WANTED
IF sy-subrc = 0.
READ DATASET lv_path INTO lv_xstring. "#EC CI_USE_WANTED
ENDIF.

CLOSE DATASET lv_path. "#EC CI_USE_WANTED

IF sy-subrc = 0.
ls_stream-mime_type = 'application/x-zip-compressed'. "mime type
ls_stream-value = lv_xstring. "content
ls_lheader-name = 'Content-Disposition'(001).

CONCATENATE 'attachment; filename="'(002)
ls_package-filename
'";'
INTO ls_lheader-value.

set_header( is_header = ls_lheader ).

ls_lheader-name = 'Content-Length'(003).

DATA(lv_len) = xstrlen( ls_stream-value ).
ls_lheader-value = lv_len.
set_header( is_header = ls_lheader ).
copy_data_to_ref( EXPORTING is_data = ls_stream
changing cr_data = er_stream ).

ELSE. "error when file not found

lo_meco = mo_context->get_message_container( ).
lo_meco->add_message_text_only(
EXPORTING
iv_msg_type = 'E'
iv_msg_text = 'File Not Found'(004) " Message Text
iv_add_to_response_header = abap_true " Flag for adding or not the message to the response header
).

ENDIF.

ENDMETHOD.

 

Step 4


4.1. Now redefine the method DEFINE of MPC extension class. Add your Entity name and field which contains the zip package.
METHOD define.
super->define( ).

DATA: lo_entity TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
lo_property TYPE REF TO /iwbep/if_mgw_odata_property.

lo_entity = model->get_entity_type( iv_entity_name = 'Package' ).

IF lo_entity IS BOUND.
lo_property = lo_entity->get_property( iv_property_name = 'Filename').
lo_property->set_as_content_type( ).
ENDIF.

ENDMETHOD.

 

Step 5


5.1. Now it's time to register the Odata service from /IWFND/MAINT_SERVICE tcode.

5.2. Once, the service is registered let's see if the metadata is loaded properly.


Metadata


 

Step 6


6.1. Lets us now upload the zip file to the application server.


Upload Package


6.2. Contents of the package. There are 52 notepad files in it.


Package contents


 

6.3. Let us execute the Odata service to get the ZIP Package. Select the HTTP method as GET and pass the below URI.
/sap/opu/odata/sap/ZZIPPACKAGE_DOWNLOAD_SRV/PackageSet('0001')/$value

 

6.4. Below is the HTTP response.


.


Response


 

6.5. Here comes the final step, click on the "Response in Browser" and let us see the uploaded ZIP package.


Downloaded ZIP Package


Hurray! the same ZIP package is downloaded.

Conclusion


In this way, we can download the ZIP package from the Odata service. UI developers can use the same service and trigger to download the package from the frontend button. Thank you for reading the blog post on ZIP Package download. I hope that you found the information useful and that it has provided you with some valuable insights.

If you have any thoughts or questions on the topic, please feel free to leave a comment below. I would love to hear from you.

If you found this post helpful, please like and share it with your network 🙂

 

Kind Regards,

Nitin Sharma
Labels in this area