Image processing in ABAP Web Dynpro using custom table

...Previous

Table of element and their property

Element ID

Element Type

Attribute

Value

Note

ROOTUIELEMENTCONTAINER

TransparentContainer

Layout

MatrixLayout

 

NAME

Label

text

Enter your Image

 

File_upload

FileUpload

data

V_MAIN.IMAGE.DATA

Bind to attribute "data" of node "Image"

File_upload

FileUpload

filename

V_MAIN.IMAGE.NAME

Bind to attribute "name" of node "Image"

BUTTON

Button

text

Save

 

LINK

Image

source

V_MAIN.IMAGE.LINK

Bind to attribute "link" of node "Image"

LINK

Image

height

120px

 

LINK

Image

width

120px

 

TABLE

Table

dataSource

V_MAIN.IMG_TABLE

Bind to node "Img_table"

TABLE

Table

colSpan

10

 

TABLE_ID

TableColumn

     

TABLE_ID_EDITOR

TextView

text

V_MAIN.IMG_TABLE.ID

Bind to attribute "id" of node "Img_table"

TABLE_ID_HEADER

Caption

text

ID

 

TABLE_URL

TableColumn

     

TABLE_URL_EDITOR

Image

text

V_MAIN.IMG_TABLE.URL

Bind to attribute "url" of node "Img_table"

TABLE_URL_EDITOR

Image

height

50px

 

TABLE_URL_EDITOR

Image

width

50px

 

TABLE_URL_HEADER

Caption

text

Image

 

TABLE_HEADER

Caption

text

Images

 

Select button save and create event “save” by clicking button (create event) against property “onAction”.

Click on attribute tab and create attribute “GV_LAST_ID” of type “int2”

Click on method tab and create method “LOAD_IMAGE”

Double click on method “WDDOINIT” paste following code

METHOD wddoinit.
  wd_this
->load_image(
  
).
ENDMETHOD.

Double click on method “ONACTIONSAVE” paste following code

METHOD onactionsave.
*   get message manager
  
DATAlo_api_controller   TYPE REF TO if_wd_controller,
        lo_message_manager  
TYPE REF TO if_wd_message_manager,
        lv_msg_text         
TYPE        string,
        lv_msg_type         
TYPE        i,
        lv_file_name        
TYPE        string,
        lv_lfname           
TYPE        chkfile,
        lv_ext              
TYPE        sdba_funct,
        lo_nd_image         
TYPE REF TO if_wd_context_node,
        lo_el_image         
TYPE REF TO if_wd_context_element,
        ls_image            
TYPE        wd_this->element_image,
        cached_response     
TYPE REF TO if_http_response,
        lv_file_content     
TYPE        xstring,
        lv_guid             
TYPE        guid_32,
        lv_host             
TYPE        string,
        lv_port             
TYPE        string,
        lv_protocol         
TYPE        string,
        lv_url              
TYPE        string,
        ls_img              
TYPE        ztable_image
        
.

  
IF wd_this->gv_last_id IS INITIAL.
    wd_this
->gv_last_id 1.
  
ENDIF.
  lo_api_controller ?= wd_this
->wd_get_api( ).

  
CALL METHOD lo_api_controller->get_message_manager
    RECEIVING
      message_manager 
lo_message_manager.

*   navigate from <CONTEXT> to <IMAGE> via lead selection
  lo_nd_image 
wd_context->get_child_nodename wd_this->wdctx_image ).

*   get element via lead selection
  lo_el_image 
lo_nd_image->get_element( ).

*   get all declared attributes
  lo_el_image
->get_static_attributes(
    
IMPORTING
      static_attributes 
ls_image ).

  
IF ls_image-name IS INITIAL.
    lv_msg_text 
'Select any Image then click save button'.
    lv_msg_type 
2.
  
ELSE.
    
CALL FUNCTION 'TRINT_SPLIT_FILE_AND_PATH'
      
EXPORTING
        full_name     
ls_image-name
      
IMPORTING
        stripped_name 
lv_file_name
*       FILE_PATH     = FILE_PATH
      
EXCEPTIONS
        x_error       
1
        
OTHERS        2.
    
IF sy-subrc <> 0.
      lv_msg_text 
'System error: while reading file location'.
      lv_msg_type 
3.
    
ELSE.
      
SHIFT lv_file_name LEFT DELETING LEADING '0'.
      lv_lfname 
lv_file_name.
      
CALL FUNCTION 'SPLIT_FILENAME'
        
EXPORTING
          long_filename  
lv_lfname
        
IMPORTING
*         PURE_FILENAME  = PURE_FILENAME
          pure_extension 
lv_ext.
      
TRANSLATE lv_ext TO LOWER CASE.

      
IF lv_ext 'png' OR lv_ext 'gif' OR lv_ext 'jpeg' OR lv_ext 'jpg' OR lv_ext 'bmp'.
        
CREATE OBJECT cached_response
          
TYPE
          cl_http_response
          
EXPORTING
            add_c_msg 
1.
        lv_file_content 
ls_image-data.
        cached_response
->set_datalv_file_content ).
        cached_response
->set_header_fieldname  if_http_header_fields=>content_type
                                           
value 'image/jpeg' ).
* Create a unique URL for the object
        
CALL FUNCTION 'GUID_CREATE'
          
IMPORTING
            ev_guid_32 
lv_guid.
*

***Set the Response Status
        cached_response
->set_statuscode 200 reason 'OK' ).

***Set the Cache Timeout - 60 seconds - we only need this in the cache
***long enough to build the page and allow the IFrame on the Client to request it.
        cached_response
->server_cache_expire_relexpires_rel 360 ).

        
CALL METHOD cl_http_server=>get_location
          
EXPORTING
            server       
cl_wdr_task=>server
          
IMPORTING
            host         
lv_host
            port         
lv_port
            out_protocol 
lv_protocol.

        
CONCATENATE
          lv_protocol 
'://' lv_host ':' lv_port '/sap/bc/webdynpro/sap/'
          lv_guid 
'.JPG'
          
INTO lv_url.

        cl_http_server
=>server_cache_uploadurl      lv_url
                                             response 
cached_response ).

*         set single attribute
        lo_el_image
->set_attribute(
          name 
=  `LINK`
          
value lv_url ).

        ls_img
-id         wd_this->gv_last_id.
        ls_img
-image_data ls_image-data.
        
MODIFY ztable_image FROM ls_img.
        
IF sy-subrc <> 0.
          lv_msg_text 
'DB error: Unable save data'.
          lv_msg_type 
3.
        
ELSE.
          wd_this
->load_image(
          
).
        
ENDIF.
      
ELSE.
        lv_msg_text 
'Extension error: not supported file type'.
        lv_msg_type 
2.
      
ENDIF.
    
ENDIF.
  
ENDIF.
  
IF lv_msg_text IS NOT INITIAL.
*   report message
    
CALL METHOD lo_message_manager->report_message
      
EXPORTING
        message_text 
lv_msg_text
        message_type 
lv_msg_type.
  
ENDIF.

ENDMETHOD.

Click here to continue...

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image