Skip to Content
Author's profile photo Amy King

Upload and Display an Image at Runtime

Scenario

You need to allow users to upload an image from their local workstation and immediately view the image in Web Dynpro.

Procedure

1.0 Create a Web Dynpro Component

Create a Web Dynpro component, ZDEMO_UPLOAD_IMAGE with a view, V_MAIN and window, W_MAIN. Also create an application for the component.

demo1.JPG

2.0 Create a Context Node

In the component controller, create a context node DEMO with cardinality 1..1 and selection 0..1. Add the following three attributes to the node.

Attribute Name Type
FILEUPLOAD_DATA XSTRING
CONTENT_TYPE STRING
IMAGE_SOURCE STRING

3.0 Build the View

In view V_MAIN, map component controller context node DEMO to the view’s context. Add the following three UI elements to the view and bind their properties to the context as indicated.

FileUpload

Properties (FileUpload)
ID FILEUPLOAD
data V_MAIN.DEMO.FILEUPLOAD_DATA
mimeType V_MAIN.DEMO.CONTENT_TYPE

Button

Properties (Button)
ID BUTTON
text Upload
Events
onAction UPLOAD

Image

Properties (Image)
ID IMAGE
source V_MAIN.DEMO.IMAGE_SOURCE

4.0 Implement the Image Upload Action

In the handler method for action UPLOAD, implement the following code to temporarily store the image in an HTTP response object just long enough to bind the view’s IMAGE source property to the cached image.

METHOD onactionupload .

   DATA: BEGIN OF ls_image,
                  path TYPE string VALUE '/sap/public/',
                  uuid TYPE sysuuid_c22,
                  url    TYPE string,
               END OF ls_image.

   DATA lo_http_response TYPE REF TO if_http_response.
   DATA lo_nd_demo         TYPE REF TO if_wd_context_node.
   DATA lo_el_demo          TYPE REF TO if_wd_context_element.
   DATA ls_demo               TYPE wd_this->element_demo.

 * -- Read uploaded image attributes from context

   lo_nd_demo = wd_context->get_child_node( name = wd_this->wdctx_demo ).
   lo_el_demo = lo_nd_demo->get_element( ).

   lo_el_demo->get_static_attributes(
       IMPORTING
           static_attributes = ls_demo ).

 * -- Place the image into the HTTP response cache

 * Create an HTTP response object
   CREATE OBJECT lo_http_response
       TYPE cl_http_response
       EXPORTING
           add_c_msg = 1. " new pointer

 * Add the image's binary data and its content type to the HTTP response object
   lo_http_response->set_data( ls_demo-fileupload_data ).

   lo_http_response->set_header_field(
       name  = if_http_header_fields=>content_type
       value   = ls_demo-content_type
   ).

 * Create a unique URL for the image
   TRY.
           ls_image-uuid = cl_system_uuid=>create_uuid_c22_static( ).
       CATCH cx_uuid_error.
   ENDTRY.

   CONCATENATE ls_image-path ls_image-uuid INTO ls_image-url.

 * Allow a sufficient window of time to fetch the image from cache
   lo_http_response->server_cache_expire_rel( expires_rel = 60 ).

 * Place the image into cache
   cl_http_server=>server_cache_upload(
       url             = ls_image-url
       response = lo_http_response
   ).

 * -- Set image source in context

   lo_el_demo->set_attribute(
       name  = `IMAGE_SOURCE`
       value   = ls_image-url
   ).

 ENDMETHOD.

Result

Save, activate and run the Web Dynpro application. You may now upload any image format and display it in the Web Dynpro.

demo_dice2.JPG

demo_penguins2.JPG

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Amy, Thanks for sharing.

      Author's profile photo Former Member
      Former Member

      Hi,

      This image is saved temporarily in the cache. How can I save it in the database to reload the image after the WD app has beed closed ?

      Thank you .

      Rachid.

      Author's profile photo Amy King
      Amy King
      Blog Post Author

      Hi Rachid,

      You can achieve this with a File Upload UI element. If you do a search in the SCN web dynpro forum for "upload image", you should find a few examples.

      Cheers.

      Author's profile photo Former Member
      Former Member

      Hi Amy,

      yes, I have followed your great topic. Thank you.

      My images are uploaded and displayed correctly in my webdynpro via the "FileUpload UI element" (are you speaking about the same component?) that you specify in your topic.

      But I do not know, how to store definitively my image (url) in the system (database) and not only in the cache.

      /wp-content/uploads/2015/05/4_712583.jpg

      /wp-content/uploads/2015/05/3_712584.jpg

      Can you clarify?

      Thank again Amy.

      Rachid.

      Author's profile photo Amy King
      Amy King
      Blog Post Author

      Hi Rachid,

      Yes, it's the same UI element. Here is one example of how to store and later retrieve a file you've uploaded, Attach files with save, retrieve and delete functionality in Web Dynpro ABAP - Part 1

      Cheers,

      Amy

      Author's profile photo Former Member
      Former Member

      Hi Amy!

      Great, thank you a lot!

      Cheers,

      Rachid.

      Author's profile photo Nad Cuky
      Nad Cuky

      Class CL_SYSTEM_UUID does not exist! How can I get the same functionality?

      Author's profile photo Amy King
      Amy King
      Blog Post Author

      Hi Nadav,

      This class is available in NetWeaver 7.00 EHP 2 and SAP NetWeaver 7.10. Please take a look at note 935047 - Creating and using GUIDs (UUIDs) for function module alternatives.

      Cheers,

      Amy

      Author's profile photo Nad Cuky
      Nad Cuky

      Hi Amy,

      Unfortunately I don't have access to this link! Can you provide another one?

      Thank you!

      Author's profile photo Amy King
      Amy King
      Blog Post Author

      Hi Nadav,

      Function module alternatives include:

      • GUID_CREATE
      • SYSTEM_UUID_C22_CREATE
      • SYSTEM_UUID_CREATE
      • SYSTEM_UUID_C_CREATE

      Cheers.

      Author's profile photo gunjan singh
      gunjan singh

      Hi Amy,

      Thank you for sharing. it’s very useful. ?
      I am getting an error at URL that “URL is not correct”. I don’t know where I am wrong.

      Author's profile photo Austin D costa
      Austin D costa

      Thanks for the clear and crisp explanation.

      -Auzz