This is about embedding a picture that is stored in the MIME repository in an HTML-file. I will show two ways of accessing the picture:

  • using an API
  • using the ICF

There are many ways of displaying an HTML file. Again I will show two ways:

  • using the SAP GUI Control Framework
  • using the ICF

The methods for accessing the MIME repository and displaying the HTML file can be combined.

The code examples shown in the following partly use ABAP 7.40 syntax. To run on 7.31, 7.03, 7.02, 7.0 you have to replace some expressions with the respective statements.

Accessing the MIME Repository with an API

In order to get a picture stored as a node of the MIME repository into an ABAP program you simply use code as follows (before 7.40, you have to declare DATA img TYPE xstring before the method call instead ot the inline declaration):

cl_mime_repository_api=>get_api( )->get(
        EXPORTING i_url = `/SAP/PUBLIC/BC/ABAP/mime_demo/ABAP_Docu_Logo.gif`
        IMPORTING e_content = DATA(img) ).

After this, the xstring img contains the GIF-picture passed to the GET-method.

Using the Picture in the SAP GUI Control Framework

After loading the picture into ABAP, you can embed it in an HTML-file shown in the browser control of the SAP GUI Control Framework (CFW). To do so,you must load it into the browser control. Unfortunately, method LOAD_DATA of CFW class CL_GUI_HTML_VIEWER was created in Release 4.6 and does not accept strings that were added to ABAP in Release 6.10. Therefore, you have to convert the above xstring to an internal table first:

TYPES: pict_line(1022) TYPE x,

       pict_tab TYPE STANDARD TABLE OF pict_line WITH EMPTY KEY.

DATA(length) = xstrlen( img ).

WHILE length >= 1022.

  APPEND img(1022) TO pict_tab.

  SHIFT img BY 1022 PLACES LEFT IN BYTE MODE.

    length = xstrlen( img ).

ENDWHILE.

IF length > 0.

  APPEND img TO pict_tab.

ENDIF.

Now you can load it:

DATA pict_url TYPE c LENGTH 255.

DATA(html_control) = NEW

     cl_gui_html_viewer( parent = custom_container ).

html_control->load_data(

  EXPORTING

    url          = ‘picture_url’

    type         = ‘image’

    subtype      = ‘.gif’

  IMPORTING

    assigned_url = pict_url

  CHANGING

    data_table   = pict_tab ).

The URL returned by that method in pict_url can be used in an HTML-File loaded into the same browser control:

DATA(html_tab) = VALUE html_tab(

  ( ‘<html><body><basefont face=”arial”>’ )

  ( ‘Picture with CL_GUI_HTML_VIEWER<br><br>’ )

  ( ‘<img src=”‘ && pict_url && ‘”>’ )

  ( ‘</body></html>’ ) ).

html_control->load_data(

  IMPORTING

   assigned_url = html_url

  CHANGING

    data_table   = html_tab ).

html_control->show_url(

   EXPORTING

     url = html_url ).

Before Release 7.40 you have to APPEND the lines to html_tab of course.

Since Release 7.02 there is also the convenience class CL_ABAP_BROWSER, that encapsulates CL_GUI_HTML_VIEWER and does the loading for you:

DATA(ext_data) =

  VALUE cl_abap_browser=>load_tab( ( name = PICT.GIF

                                     type = ‘image’

                                     dref = REF #( pict_tab ) ) ).

DATA(html_tab) = VALUE cl_abap_browser=>html_table(

      ( ‘<html><body><basefont face=”arial”>’ )

      ( ‘Picture with CL_ABAP_BROWSER<br><br>’ )

      ( ‘<img src=”PICT.GIF“>’ )

      ( ‘</body></html>’ ) ).

cl_abap_browser=>show_html( html        = html_tab

                                     data_table  = ext_data ).

You only have to pass a reference to the picture table to method SHOW_HTML that is the assigned the same name as it is used in the HTML-File.

Using the Picture in ICF

In order to display an HTML-file using the Internet Connection Framework (ICF), you can create an HTTP service in transaction ICF and provide the HTML-file in the handler class of that service. The following  method implementation sends data depending on the form field pict_flag of the URL. If pict_flag is not X, an HTML-file is sent, where the address of the picture is the current address concatenated to the form field pict_flag with value X. If pict_flag is X, the picture in our xstring img is sent. With other words, we use the same HTTP-Service to send the HTML as well as the picture embedded in the HTML.

METHOD if_http_extension~handle_request.

  DATA(pict_flag) = server->request->get_form_field( name = `pict_flag` ).

  IF pict_flag <> abap_true.
    DATA(picture) =
      get_address( server = server
                   application = `/sap/bc/abap/demo_mime` )  && `?pict_flag=X”`.
    DATA(html) =
      `<html><body><basefont face=”arial”>`     &&
      `Picture from API<br><br>`                &&
      `<img src=”` && picture && `”>`            &&
      `</body></html>`.
     server->response->set_cdata( data = html ).
  ELSE.
    server->response->set_data( data = img ).
  ENDIF.

ENDMETHOD.

The helper method get_address looks as follows:

METHOD get_address.

  cl_http_server=>get_location(

     EXPORTING

       server      = server

       application = application

     IMPORTING

       host         = DATA(host)

       port         = DATA(port)

       out_protocol = DATA(protocol) ).

  IF host IS NOT INITIAL AND port IS NOT INITIAL AND protocol IS NOT INITIAL.

    address = protocol && ‘://’ && host && ‘:’ && port && application.

  ENDIF.

ENDMETHOD.

Accessing the MIME Repository with ICF

As Sandra Rossi pointed it out: If a path in ICF is built in the same way as a path in the MIME repository and if an HTTP-service in ICF is handled by CL_HTTP_EXT_WEBDAV_PUBLIC, you can directly use the URL of that service to address the corresponding MIME objects (with other words, the predefined handler sends the contents of the MIME object as we have done in our own method above).

Accessing ICF from CFW

If you have such an ICF node addressing a picture, you can use it in HTML-files shown in CFW.

DATA icf_node TYPE string.

CONSTANTS path TYPE string VALUE `/sap/public/bc/abap/mime_demo`.
   
cl_http_server=>get_location(
  EXPORTING
    application = path
  IMPORTING
    host         = DATA(host)
    port         = DATA(port)
    out_protocol = DATA(protocol) ).
IF host     IS NOT INITIAL AND
   port     IS NOT INITIAL AND
   protocol IS NOT INITIAL.
  icf_node = protocol && ‘://’ && host && ‘:’ && port && path.
ENDIF.

DATA(html) =
  `<html><body><basefont face=”arial”>`                &&
  `Picture from ICF<br><br>`               &&
  `<img src=”` && icf_node && `/ABAP_Docu_Logo.gif”>`  &&
  `</body></html>`.

cl_abap_browser=>show_html( html_string = html ).

Accessing ICF from ICF

Of course and last but not least you can embed a picture addressed with ICF in an HTML sent by an HTTP-handler in ICF.

METHOD if_http_extension~handle_request.

  DATA(picture) =
    get_address( server = server
                 application = `/sap/public/bc/abap/mime_demo` )  &&
   `/ABAP_Docu_Logo.gif”`.

    DATA(html) = `<html><body><basefont face=”arial”>`     &&
                 `Picture from ICF<br><br><br><br>`        &&
                 `<img src=”` && picture && `”>`           &&
                 `</body></html>`.

    server->response->set_cdata( data = html ).

ENDMETHOD.

The helper method get_address is the same as above.

I guess, you have to play around with that a bit.

The working examples I copied from above will be delivered with 7.40, SP05 and the output always looks like something as follows:

/wp-content/uploads/2013/08/pict_257206.gif

PS:

As a third state of cruelty you store also the HTML-file in the MIME repository and access it either by the API or by ICF.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply