Recognizing file name and type in browser after downloading file from Netweaver Gateway
Let’s consider that we have working solution of file download from gateway as it is described for example in this blog How to Read Photo from SAP system using SAP Gateway.
When we point to the file, browsers does not handle the file name correctly, as they consider $value as name of the file. Thus it is not comfortable to open the file. Also, file name is shown as $value too which is inconvenient for the user when he wants to save the file.
To solve this issue we can set content-disposition response header according to rfc 1806 to inline and set the name of file there. Then browser will be able to recognize the name, user can save it with the name as it is set in SAP system.
Here is the sample code of method GET_STREAM implementation:
METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream. DATA: ls_stream TYPE ty_s_media_resource, er_entity TYPE REF TO data, lr_entity TYPE REF TO data, ls_file TYPE cl_sample_mpc=>ts_file, ls_lheader TYPE ihttpnvp. CASE iv_entity_name. WHEN 'File'. * Load ls_file info here ls_stream-value = ls_file-content. ls_stream-mime_type = ls_file-mime_type. ls_lheader-name = 'Content-Disposition'. ls_lheader-value = 'inline; filename="'. CONCATENATE ls_lheader-value ls_file-file_name INTO ls_lheader-value. CONCATENATE ls_lheader-value '"' INTO ls_lheader-value. set_header( is_header = ls_lheader ). copy_data_to_ref( EXPORTING is_data = ls_stream CHANGING cr_data = er_stream ). ENDCASE. ENDMETHOD.
UPDATE: Escaping characters
If the file name contains specials characters, or just space, it is necessary to escape characters in the backend. To do so, following method can be used:
ls_file-file_name = escape( val = ls_file-file_name format = cl_abap_format=>e_url ).