cancel
Showing results for 
Search instead for 
Did you mean: 

file download

Former Member
0 Kudos

Hi all,

i want to fetch records from mara table and download it to notepad. please let me know how to code..

thanks

swapna

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Swapna,

You can write the below code inside the event handler. I used is for download as Excel, for download as text change the MIME-type & the file name

Data: lt_data type table of zst_record,
      ls_data like line of lt_data.

ln_node->get_static_attributes_table(
      importing
        table = lt_data ).

  " Add the headers.
  concatenate data_string
                                      'RECORDID'
                                      'PO Number'
                                      'CQN'
                                      'Customer'
                                      'Qual. Code'
                                      'CANDNO'
                                      'FNAME'
                                      'INITS'
                                      'LNAME'
                                      'GESCH'
                                      'DOB'
                                       cl_abap_char_utilities=>newline into data_string
                                      separated by cl_abap_char_utilities=>horizontal_tab.
" add the content.
  loop at lt_data into ls_data.
    concatenate data_string
                ls_data-recordid
                ls_data-bstkd
                ls_data-cqn
                ls_data-kunnr
                ls_data-matnr
                ls_data-candno
                ls_data-fname
                ls_data-inits
                ls_data-lname
                ls_data-gesch
                ls_data-gbdat
                cl_abap_char_utilities=>newline into data_string
                                        separated by cl_abap_char_utilities=>horizontal_tab.
  endloop.


  call function 'SCMS_STRING_TO_XSTRING'
    exporting
      text   = data_string
    importing
      buffer = data_xstring.

"change to correct mime type & file name for txt.
  wdr_task=>client_window->client->attach_file_to_response(  i_filename  = 'TEMP.xls'
                                                             i_content   = data_xstring
                                                             i_mime_type = 'EXCEL' ).

Greetings

Prashant

Answers (2)

Answers (2)

uday_gubbala2
Active Contributor
0 Kudos

Hi Swapna,

Below is the solution if you aren't to use the FileDownload UI element.

1) First read the table's data into an internal table.

2) Convert the internal table data to STRING format.

3) Now convert it into tab separated format as how desired.

4) Convert this STRING format to XSTRING format

5) Make use of the attach_file_to_response method.

Regards,

Uday

METHOD onactionon_submit .
  DATA: lv_node TYPE REF TO if_wd_context_node,
        lt_mara TYPE if_main=>elements_mara,
        wa_mara TYPE if_main=>element_mara,
        lead_selection_index TYPE i,
 
        mara_string  TYPE string,
        mara_xstring TYPE xstring.
 
  lv_node = wd_context->get_child_node( name = 'MARA' ).
  CALL METHOD lv_node->get_static_attributes_table
    IMPORTING
      table = lt_mara.
 
  LOOP AT lt_mara INTO wa_mara.
    CONCATENATE mara_string
                wa_mara-matnr
                wa_mara-ersda
                wa_mara-ernam
                wa_mara-matkl
                wa_mara-meins
                cl_abap_char_utilities=>newline INTO mara_string
                                        SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
  ENDLOOP.
 
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text   = mara_string
    IMPORTING
      buffer = mara_xstring.
 
 
  wdr_task=>client_window->client->attach_file_to_response(  i_filename  = 'TEMP.DOC'
                                                             i_content   = mara_xstring
                                                             i_mime_type = 'WORD' ).
ENDMETHOD.

The above is the code to export the Internal Table to Word Document. You can proceed as shown below for Excel & NOTEPAD formats.

To Export the Internal Table to Text File:

WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(
    I_FILENAME    = 'WDP.txt'
    I_CONTENT     =  mara_xstring
    I_MIME_TYPE   = 'NOTEPAD' ).

To Export the Internal Table to Excel File:

WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(
    I_FILENAME    = 'Excel.xls'
    I_CONTENT     =  mara_xstring
    I_MIME_TYPE   = 'EXCEL' ).

Former Member
0 Kudos

thanks to all...ur responses will be surely rewarded..thanks for ur efforts..

but i have two more questions from ur responses.

1. Uday the code you have sent is working for word and excel but if i use notepad, all the records are

coming in a single line. how to resolve this?

2.In the code for filedownload in wdr_test_events, the 'node' is nowhere declared but they used

node->bind_structure( struct ). Is the term 'node' is local to the view? Though my internal table is having 5 records(have debugged) i could able to display only one record if i use node->bind_table( itab ). If it is the problem with the cardinality of the node..where should i change this( node is not declared anywhere and i tried changing the cardinality of all the existing nodes to 0..n also 1..n ).

pls clarify...

thanks

swapna

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

For the all in one line in notepad problem, try changing from cl_abap_char_utilities=>newline to cl_abap_char_utilities=>cr_lf.

Also you should never direclty use

wdr_task=>client_window->client->attach_file_to_response as suggested above. This approach is not released for customer usage. Instead there is a public API that you should use: CL_WD_RUNTIME_SERVICES=>ATTACH_FILE_TO_RESPONSE. And yes it is just a wrapper around wdr_task=>client_window->client->attach_file_to_response, but SAP does this on purpose. There is no promise that WDR_TASK might not change over time since this is an internal framework object. If you direclty access it, your applications might break in the future if SAP redesigns this object. However SAP will make sure that CL_WD_RUNTIME_SERVICES continues to work, even if we change the way that WDR_TASK works.

Former Member
0 Kudos

Thomas,

i have tried both approach and both are working. but have one issue with both approach.

issue: every time i click on download button (of MS Doc file), the size is doubling. for example. if the size of the file on first download is 10 kb , then on second time it will be 20 kb.

the strange part is : number of page and contant remains the same, and this behaviour is not consistant with .PDF files. !!

do you think for MS Doc Mime type we have to do something special ?

former_member402443
Contributor
0 Kudos

Hi Swapna,

Please go thru the WDA Component - WDR_TEST_EVENTS in ur SAP System.

It will help u in undertanding the functionality of file_upload and file_download.

Regards

Manoj Kumar

Former Member
0 Kudos

but what was the supply function used in that...

i could able to display only one record in my notepad..i changed the cardinality to 0..n and binded the table yet only one record is displaying..

former_member402443
Contributor
0 Kudos

Hi

The Supply Function is used for passing some dummy data.

Regards

Manoj Kumar

Former Member
0 Kudos

please let me know the steps in coding that..im failed to download more than a record into my notepad using that..