ABAP BTP Cloud, Download files from ABAP program to local PC

Estimated read time 6 min read

As my ABAP BTP trial account is coming to end I needed to backup of the data that was saved in multiple databases tables. Though you could download the contents of the table using the eclipse data preview it was not suitable for me:

My table has fields with string data type and  using eclipse data preview did not download the string fields in correct format as it did not escape the special characters correctlyI wanted automatic way of taking regular backups of my data  in multiple tables using a utility program in a single of button.

 In the ABAP BTP cloud environment with Eclipse, there is no known way of downloading a file from an ABAP program to a local PC. The on-premise solutions like using cl_gui_frontend_services do not work in ABAP cloud with Eclipse ADT. The linked ABAP repository builds the functionality in two steps:

 Save the file contents as RAWSTRING in database table zfio_files

Use table zfio_files to generate an ABAP RAP based Fiori Elements UI. The Fiori application streams the contents of the file from database on the UI as an attachment from where users can download the file.

Note: The purpose of this blog is not to provide any functionality related to conversion of data into CSV and the demo programs does it for testing purpose only. 

Usage

Clone or Download the github repo using abapGit Plugin

Store file contents in your ABAP program to database using the provided ABAP method create_files of class ZFIO_FILES_SAVE_TO_DB. Check demo class ZCL_FIO_FILE_SAVE_DEMO which can be run as application with eclipse adt 

TYPES: BEGIN OF sample_row,

book_name TYPE c LENGTH 128,
author TYPE c LENGTH 128,
rating TYPE p LENGTH 2 DECIMALS 2,
END OF sample_row.

DATA files TYPE zfio_tt_files.
DATA sample_data TYPE STANDARD TABLE OF sample_row.

sample_data = VALUE #(
( book_name = ‘The Psychology of Money’ author = ‘Morgan Housel’ rating = ‘4.3’ )
( book_name = ‘The Millionaire Next Door’ author = ‘Thomas J. Stanley, William D. Danko’ rating = ‘4.04’ )
( book_name = ‘Thinking, Fast and Slow’ author = ‘Daniel Kahneman’ rating = ‘4.17’ ) ).

DATA(csv_string) = convert_to_csv( sample_data ).

DATA(books_xtring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( csv_string ).

files = VALUE #( ( file_mimetype = ‘text/csv’ file_name = ‘books.csv’ file_content = books_xtring ) ).
DATA(file_save_obj) = NEW zfio_files_save_to_db( ).

TRY.
file_save_obj->create_files( files = files ).
out->write( ‘Records Updated Use Service Binding ZFIO_UI_FILES_OV4 to Launch the Fiori App’ ).
CATCH zcm_fio_checks INTO DATA(fio_error).

out->write( fio_error->get_text( ) ).

ENDTRY.Access the Fiori application to view and download the files using service binding ZFIO_UI_FILES_OV4 .Download the file from Fiori UI by clicking on field Attachment. 

On clicking the attachment icon a file dialogue will appear prompting to save the file to your local PC.

The contents of file are correctly transferred into the downloaded file

Other thoughts:

The functionality has been tested on csv files but it should also work on other file types like photos or pdf as long as you are saving the contents of file in RAWSTRING  format which is hexadecimal format.

 

 

​ As my ABAP BTP trial account is coming to end I needed to backup of the data that was saved in multiple databases tables. Though you could download the contents of the table using the eclipse data preview it was not suitable for me:My table has fields with string data type and  using eclipse data preview did not download the string fields in correct format as it did not escape the special characters correctlyI wanted automatic way of taking regular backups of my data  in multiple tables using a utility program in a single of button. In the ABAP BTP cloud environment with Eclipse, there is no known way of downloading a file from an ABAP program to a local PC. The on-premise solutions like using cl_gui_frontend_services do not work in ABAP cloud with Eclipse ADT. The linked ABAP repository builds the functionality in two steps: Save the file contents as RAWSTRING in database table zfio_files. Use table zfio_files to generate an ABAP RAP based Fiori Elements UI. The Fiori application streams the contents of the file from database on the UI as an attachment from where users can download the file.Note: The purpose of this blog is not to provide any functionality related to conversion of data into CSV and the demo programs does it for testing purpose only. UsageThe code is hosted on https://github.com/ajayrawatsap/ZFIO_UTILSClone or Download the github repo using abapGit PluginStore file contents in your ABAP program to database using the provided ABAP method create_files of class ZFIO_FILES_SAVE_TO_DB. Check demo class ZCL_FIO_FILE_SAVE_DEMO which can be run as application with eclipse adt TYPES: BEGIN OF sample_row,

book_name TYPE c LENGTH 128,
author TYPE c LENGTH 128,
rating TYPE p LENGTH 2 DECIMALS 2,
END OF sample_row.

DATA files TYPE zfio_tt_files.
DATA sample_data TYPE STANDARD TABLE OF sample_row.

sample_data = VALUE #(
( book_name = ‘The Psychology of Money’ author = ‘Morgan Housel’ rating = ‘4.3’ )
( book_name = ‘The Millionaire Next Door’ author = ‘Thomas J. Stanley, William D. Danko’ rating = ‘4.04’ )
( book_name = ‘Thinking, Fast and Slow’ author = ‘Daniel Kahneman’ rating = ‘4.17’ ) ).

DATA(csv_string) = convert_to_csv( sample_data ).

DATA(books_xtring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( csv_string ).

files = VALUE #( ( file_mimetype = ‘text/csv’ file_name = ‘books.csv’ file_content = books_xtring ) ).
DATA(file_save_obj) = NEW zfio_files_save_to_db( ).

TRY.
file_save_obj->create_files( files = files ).
out->write( ‘Records Updated Use Service Binding ZFIO_UI_FILES_OV4 to Launch the Fiori App’ ).
CATCH zcm_fio_checks INTO DATA(fio_error).

out->write( fio_error->get_text( ) ).

ENDTRY.Access the Fiori application to view and download the files using service binding ZFIO_UI_FILES_OV4 .Download the file from Fiori UI by clicking on field Attachment. On clicking the attachment icon a file dialogue will appear prompting to save the file to your local PC.The contents of file are correctly transferred into the downloaded fileOther thoughts:The functionality has been tested on csv files but it should also work on other file types like photos or pdf as long as you are saving the contents of file in RAWSTRING  format which is hexadecimal format.    Read More Technology Blogs by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author