Friday, 16 June 2017

Step by step process to upload and download document in webdynpro abap

This document will provide the step by step process from uploading a pdf document from presentation server to application server and save the document from application server to presentation server.

Contents

  1. Target Users
  2. Business Requirement
  3. Screen Details
  4. Process to Upload the document in webdynpro pop up screen.
  5. Display the data from the POP up if required
  6. Download the PDF document from Application Server

1: Target Users : The target users of this document is those ABAPers working primarily in HR ABAP area and for them who were facing a situation of storing  the document in application server and retrieve it rather than storing the data in a table .

2: Business Requirement

The business requirement is involved in two parts.

Part1–>This part is for the employees who have been allocated certain courses where they will finish the courses and upload the documents related to the courses completed by them.

Part2–>This part is for the Team who will validate those document and could able to download and view the documents attached by the employee, verify them and put their comments accordingly.

Part 1 Steps–>

Screen Details–>

SAP ABAP Web Dynpro, SAP

The above screen show a highlighted button which will help the user to upload the document as he/she has already completed the course.

SAP ABAP Web Dynpro, SAP

This screen will help the user to upload the document and once he/she hit the attach button it will save in the Application server.

Here I am not providing the details how to trigger the pop-up screen.

Below code is about saving the document in application server
CONSTANTS path_name TYPE localfile VALUE ‘/usr/sap/HRD/Learning/’.

“==========================================================
“Loop over attachment list for only new attachments
“==========================================================
LOOP AT lt_attachment_list ASSIGNING <fs_attachment_list> .
*    WHERE status = abap_false.” new attachment

MOVE-CORRESPONDING <fs_attachment_list> TO ls_data.
APPEND ls_data TO lt_data.
ENDLOOP.
“get message manager
lo_api_controller ?= wd_this->wd_get_api( ).
” get message manager reference
CALL METHOD lo_api_controller->get_message_manager
RECEIVING
message_manager = lo_msg_manager.
IF lt_data[] IS INITIAL.
” Attachement list is already saved
lo_msg_manager->report_error_message(
message_text = ‘Attachement list is already saved’ ).
RETURN.
ENDIF.

*—————————————————————-*
* Save the data in application server
*—————————————————————-*
CONCATENATE path_name ls_data–file_name into file.
lv_filedata = ls_data–file_data.
OPEN DATASET file FOR OUTPUT IN BINARY MODE MESSAGE msg.
TRANSFER ls_data–file_data TO file.
IF sy–subrc = 0.
CLOSE DATASET file.
ENDIF.

*—————————————————————-*
” Show success message
lo_msg_manager->report_success(
message_text = ‘Attachement list saved successfully !!!’ ).

SAP ABAP Web Dynpro, SAP

Document Mapped

SAP ABAP Web Dynpro, SAP

Hit the attach button and the document saved successfully in the Application Server.

SAP ABAP Web Dynpro, SAP

View the PDF File–>

To view the PDF document which user attached he has to click on the file name hyper link.

SAP ABAP Web Dynpro, SAP

Code to View the PDF document–>

*———————————————————————————–*
* For Upload Context Node
*———————————————————————————–*
lo_nd_file_upload = wd_context->get_child_node( name = wd_this->wdctx_file_upload ).
* get element via lead selection
lo_el_file_upload = lo_nd_file_upload->get_element( ).
lo_el_file_upload->get_static_attributes(
IMPORTING
static_attributes = ls_input ).

if ls_input IS NOT INITIAL.
wd_comp_controller->GV_PDF = ls_input–file_contents.
else.
*———————————————————————————–
*     Select the data from the database table to view as the PDF     *
*———————————————————————————–
LOOP AT lt_elements INTO lo_element.
lo_element->get_static_attributes(
IMPORTING
static_attributes = ls_attach_list
).
“Check if record exists in table
MOVE-CORRESPONDING ls_attach_list TO ls_data.
APPEND ls_data TO lt_data.
ENDLOOP.
wd_comp_controller->GV_PDF = ls_data–file_data.
endif.
wd_this->fire_out_from_popup_plg( ).
endmethod.

Part 2 Steps–>

Screen Details–>

SAP ABAP Web Dynpro, SAP

In this screen team can approve the request and can able to download the document.

Below code is about downloading the data from Application Server
CALL FUNCTION ‘EPS2_GET_DIRECTORY_LISTING’
EXPORTING
IV_DIR_NAME                  = dir_name
TABLES
DIR_LIST                     = dir_list
EXCEPTIONS
INVALID_EPS_SUBDIR           = 1
SAPGPARAM_FAILED             = 2
BUILD_DIRECTORY_FAILED       = 3
NO_AUTHORIZATION             = 4
READ_DIRECTORY_FAILED        = 5
TOO_MANY_READ_ERRORS         = 6
EMPTY_DIRECTORY_LIST         = 7
OTHERS                       = 8
.
IF SY–SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT lt_data INTO ls_data.
READ TABLE DIR_LIST  INTO wa_list with key name = ls_data–file_name.
lv_filename = ls_data–file_name.
CALL METHOD cl_wd_runtime_services=>attach_file_to_response
EXPORTING
i_filename  = lv_filename
i_content   = ls_data–file_data
i_mime_type = ‘pdf’.
ENDLOOP.

SAP ABAP Web Dynpro, SAP

Once hit the download button the document will download with download option.

1 comment: