Thursday, August 05, 2010

WebDynpro: Upload and Download file, part III (usage)

Let’s see how to make use of the download and upload components in this section. We will upload a file to show it on the browser and then change contents of the file and download it.

image Upload Screen which leads to…Download Screen

image Download Screen which goes back to…Upload screen

image Used WebDynpro components

image Controller usage

image Component list

image Window layout

image Context for Component controller

In the init of the component controller we will initiate the used components and get reference to the respective assistance class. For the download component we fill the file node with the file name and mime type.

  1. METHOD wddoinit .
  2. DATA lo_cmp_usage TYPE REF TO if_wd_component_usage.
  3. * Download component
  4. lo_cmp_usage = wd_this->wd_cpuse_download_file( ).
  5. IF lo_cmp_usage->has_active_component( ) IS INITIAL.
  6. lo_cmp_usage->create_component( ).
  7. DATA lo_ziwci_wdrc_download_file TYPE REF TO ziwci_wdrc_download_file .
  8. lo_ziwci_wdrc_download_file = wd_this->wd_cpifc_download_file( ).
  9. wd_this->o_model_download = lo_ziwci_wdrc_download_file->get_model(
  10. ).
  11. * Give file name and specify mime type
  12. DATA lo_nd_file TYPE REF TO if_wd_context_node.
  13. DATA lo_el_file TYPE REF TO if_wd_context_element.
  14. DATA ls_file TYPE wd_this->element_file.
  15. * navigate from <CONTEXT> to <FILE> via lead selection
  16. lo_nd_file = wd_context->get_child_node( name = wd_this->wdctx_file ).
  17. * get element via lead selection
  18. lo_el_file = lo_nd_file->get_element( ).
  19. ls_file-filename = 'up_down_text.txt'.
  20. ls_file-mimetype = 'Text'.
  21. * set all declared attributes
  22. lo_el_file->set_static_attributes(
  23. EXPORTING
  24. static_attributes = ls_file ).
  25. * Upload component
  26. lo_cmp_usage = wd_this->wd_cpuse_upload_file( ).
  27. IF lo_cmp_usage->has_active_component( ) IS INITIAL.
  28. lo_cmp_usage->create_component( ).
  29. DATA lo_ziwci_wdrc_upload_file TYPE REF TO ziwci_wdrc_upload_file .
  30. lo_ziwci_wdrc_upload_file = wd_this->wd_cpifc_upload_file( ).
  31. wd_this->o_model_upload = lo_ziwci_wdrc_upload_file->get_model(
  32. ).
image Events handler

Upload event handler reads the data by calling the assistance class method get_mapped_file and binds the node table_data to the returned table.

  1. METHOD catch_upload .
  2. DATA lo_nd_table_data TYPE REF TO if_wd_context_node.
  3. DATA lo_el_table_data TYPE REF TO if_wd_context_element.
  4. DATA ls_table_data TYPE wd_this->element_table_data.
  5. DATA lt_data TYPE TABLE OF wd_this->element_table_data.
  6. wd_this->o_model_upload->get_mapped_file( IMPORTING e_t_mapped_file = lt_data ).
  7. * navigate from <CONTEXT> to <TABLE_DATA> via lead selection
  8. lo_nd_table_data = wd_context->get_child_node( name = wd_this->wdctx_table_data ).
  9. lo_nd_table_data->bind_table( EXPORTING new_items = lt_data ).
Download event handler does a bit more than upload event catcher. It has to convert the data in the node table_data into file structure. It does that by calling the method covert_struc2file in the assistance class. Once the conversion is done then call the interface method download_file of download WD component that will push the file out of the browser.
  1. METHOD catch_download .
  2. DATA lo_nd_table_data TYPE REF TO if_wd_context_node.
  3. DATA lo_el_table_data TYPE REF TO if_wd_context_element.
  4. DATA ls_table_data TYPE wd_this->element_table_data.
  5. DATA lt_data TYPE TABLE OF wd_this->element_table_data.
  6. * navigate from <CONTEXT> to <TABLE_DATA> via lead selection
  7. lo_nd_table_data = wd_context->get_child_node( name = wd_this->wdctx_table_data ).
  8. * @TODO handle not set lead selection
  9. IF lo_nd_table_data IS INITIAL.
  10. * Get table data
  11. lo_nd_table_data->get_static_attributes_table( IMPORTING table = lt_data ).
  12. * Send data to be converted
  13. TRY .
  14. wd_this->o_model_download->convert_struc2file( EXPORTING i_t_data = lt_data
  15. i_seperator = cl_abap_char_utilities=>horizontal_tab
  16. i_flag_header = abap_true ).
  17. CATCH zcx_library.
  18. * get message manager
  19. DATA lo_api_controller TYPE REF TO if_wd_controller.
  20. DATA lo_message_manager TYPE REF TO if_wd_message_manager.
  21. lo_api_controller ?= wd_this->wd_get_api( ).
  22. lo_message_manager = lo_api_controller->get_message_manager( ).
  23. * report message
  24. lo_message_manager->report_error_message(
  25. message_text = 'Error in conversion'
  26. ).
  27. * Start download
  28. DATA lo_interfacecontroller TYPE REF TO ziwci_wdrc_download_file .
  29. lo_interfacecontroller = wd_this->wd_cpifc_download_file( ).
  30. lo_interfacecontroller->download_file(
  31. ).
The views are simple, they just have a view container to display the component WD and a table element to display edit. There is also button to navigate between the views.
image Upload view

image Download view