Skip to end of metadata
Go to start of metadata

A requirement I faced recently was to enhance an existing web dynpro enabling file attachment to be saved (in the existing functionality the file was being sent as an email) and then needed to display it from with in the SAP standard GUI report. The challenge was to enable all kind of files to be saved in the transparent table and then able to view them in the target application. After going through a lot of SDN blogs and searching the internet. I was able to device the following solution with the help of my colleague Subrah Kappagantula (Thanks Subrah!).

The first step I took was to save the extension, mime type and data contents as string.

The file was being attached using WD FileUpload element, so it was easy to map the properties DATA to a variable of type XSTRING, MIMETYPE to a variable of STRING and I also determined the file extension through the provided file name. But at the time of saving the data contents, instead of using a conversion routine to convert XSTRING to STRING, I simply used the move statement to move the contents.

            name = 'DATA_CONTENTS' "mapped to 'DATA' property of FileUpload element
            value = lv_filedata ).

      move lv_filedata to ls_tab-att_xstring.

              name = 'MIMETYPE'
              value = lv_mimetype ).

        move lv_mimetype to ls_tab-MIME_TYPE.

wd_context->get_attribute( exporting name = 'FILENAME' "file name to determine the extension
                                   importing value = lv_filename ).

data: dot_offset type i.
        find first occurrence of regex '\.[^\.]+$' in lv_filename match offset dot_offset.

        add 1 to dot_offset.
        ls_tab-file_ext = lv_filename+dot_offset.

and saved the ls_tab line in my transparent table along with other key information.

In the report, I wrote a bunch of code to retrieve the selected data and provided a button to view attachments for each row, where available. Upon clicking on the view attachment button, I used the following code to retrieve the attachment and display it in the native application.

move: ls_tab-att_xstring TO lx_data,
           ls_tab-mime_type TO lv_mimetype.

Convert XSTRING to BINARY data format

Data: lv_file_len type i,
         out_tab TYPE STANDARD TABLE OF tbl1024.

          buffer          = lx_data
*         APPEND_TO_TABLE = ' '
          output_length   = lv_file_len
          binary_tab      = out_tab.

Then download the file in the users SAP work directory

CONCATENATE 'TEMP_ATT.' ls_ind1-file_ext INTO lv_filename.

          filename        = lv_filename
          filesize          = lv_file_len
*         BINARY          = 'X'
*         FRONTEND        = 'X'
          mimetype        = lv_mimetype
*         P_TRANSFER_PHIO =
          data            = out_tab
          error           = 1
          OTHERS          = 2.
      IF sy-subrc <> 0.
* Implement suitable error handling here


Determine the users work directory through static method GET_SAPGUI_WORKDIR of class CL_GUI_FRONTEND_SERVICES

CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
          sapworkdir            = lv_temp_dir
          get_sapworkdir_failed = 1
          cntl_error            = 2
          error_no_gui          = 3
          not_supported_by_gui  = 4
          OTHERS                = 5.
      IF sy-subrc = 0.
        CALL METHOD cl_gui_cfw=>flush
            OTHERS = 1.
        IF sy-subrc = 0 AND NOT lv_temp_dir IS INITIAL.
*          l_dir_path = lv_temp_dir.

Once I have the directory I concatenate the directory path with the file name into the variable LV_URL, which could be then called through FM CALL_BROWSER.

      CONCATENATE 'file://' lv_temp_dir '\' lv_filename INTO lv_url.

          url                    = lv_url
          window_name            = 'Attached File'
*         NEW_WINDOW             = ' '
*         BROWSER_TYPE           =
*         CONTEXTSTRING          =
          frontend_not_supported = 1
          frontend_error         = 2
          prog_not_found         = 3
          no_batch               = 4
          unspecified_error      = 5
          OTHERS                 = 6.
      IF sy-subrc <> 0.
* Implement suitable error handling here

Try it out and please feel free to comment. I'm always open for suggestions to improve any solution!

Note: In the various blogs I looked at, there were mentions that you should convert your XSTRING to STRING first and vice verse but for me it worked without using any conversion FM. The said testing was carried out on ECC 6 EHP5.

Note II: For production SAP Standard tools should be used like the Knowledge provider. Thanks for the information Gregor Wolf.