How to show file name when calling GET_STREAM
This document is about how to enhancement our OData service – Download File.
Currently base on training material, we usually redefine GET_STREAM method and pass mime type and stream value to the result.
ls_stream–mime_type = output–return–MIME_TYPE .
ls_stream-value = output–return–DOCUMENT_STREAM .
copy_data_to_ref( EXPORTING is_data = ls_stream
CHANGING cr_data = er_stream ).
This works, however the problem is if you trigger the service in Chrome. You can only get a document with named ‘entity’.
Problem
- What if customer has already stored file name in ECM or in Database? They want to see the real name.
- What if customer don’t want to direct download, they may want to review the file before download.
Solution
Solution to this is to set a header to http framework. I attached code below.
DATA ls_lheader TYPE ihttpnvp.
“DATA lv_filename TYPE string.
“lv_filename = escape( val = lv_filename format = cl_abap_format=>e_url ).
ls_lheader–name = ‘Content-Disposition’.
ls_lheader–value = ‘outline; filename=”Mobile.pdf”;’.
set_header( is_header = ls_lheader ).
ls_stream–mime_type = output–return–MIME_TYPE .
ls_stream–value = output–return–DOCUMENT_STREAM .
copy_data_to_ref( EXPORTING is_data = ls_stream
CHANGING cr_data = er_stream ).
Let’s test the result now:
If customer want to preview the document instead of directly download, please change code as below.
ls_lheader–value = ‘inline; filename=”Mobile.pdf”;’.
Let’s test the result:
A new page was opened for preview, you can right click and choose to save. File name ‘Mobile.pdf’ comes up.
Thanks to share this useful bit of info
Thanks for document.
It does not work in ie 10 browser.
Does anyone have any suggestions?
I use below code in sapui5.
window.open("/sap/opu/odata/sap/MYSERVICE_SRV/DownloadFileSet('"+ guid +"')/$value");
Good afternoon Eric Zhao,
I just want to say THANK YOU for this SOLUTION which I used right now. Through this I was also able to learn new things. THANKS and have a GREAT DAY 🙂 🙂 🙂
Hi Eric. Outstanding post!
I need to download when the user clicks on download button. What do i need to do using your solution pls?
In local service it works, but in UI5 dont...
Best Regards.
Hi Eric. Outstanding post!
I have written same code as stated above but still file gets downloaded with name $ value . Does above code support only some browser version or fiori version .
Thanks in advance for your support .
Hi,
I have a Master-Detail app. The pdf file opens successfully in the browser as planned.
But the file is still titled
$value.pdf
, and switchingÂoutline
/inline
 has no effect.Best Regards.
the solution's link is https://blogs.sap.com/2018/01/31/sap-fiori-tricks-get-rid-of-value-in-pdf-displaydownloads/
You have to change inline to attachment to enable direct download request from your browser.
Thanks very much, @Eric Zhao your answer solve my problem wich confused me much long time. Thanks again!
Just noticed that if you want to download the file, you should choose "attachment" instead of "outline", more information here:Â https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
Just wanted to thank you! The line
fixed my wrong displayed filenames with german 'ä,ö,ü, ..'
Hi Eric .
Thank you. now its working fine, i can give any name.
thanks,
bijesh.