matinal:SAP ABAP 实现图片上传及展示等功能

2023-10-14 15:25:07 浏览数 (3)

SAP里的图片管理

这里主要谈主数据或者单据相关的图片管理,SMW0或者LOGO之类的比较简单,不在讨论之列.

图片管理之SAP系统存储、显示

需要建一个表做数据存储。 程序需要建立一个空屏幕100,跟一个简单退出的屏幕状态。

屏幕逻辑流如下:

代码语言:javascript复制
PROCESS BEFORE OUTPUT.
  MODULE status_0100.
*
PROCESS AFTER INPUT.
  MODULE user_command_0100.

主代码如下:

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report ZDEMO_PIC
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdemo_pic.
**数据定义部分

TYPES: BEGIN OF ty_pic,
         pic_data(1024) TYPE x,
       END OF ty_pic.

DATA: pic_tab TYPE TABLE OF ty_pic.

DATA: wa_pic TYPE zpictable.



**zpictable为自定义table,设置一个主键字段,再设置一个或者多个RAWSTRING类型的字段存储图片数据
DATA: g_docking_container TYPE REF TO cl_gui_docking_container,
      g_splitter          TYPE REF TO cl_gui_splitter_container,
      c_pic               TYPE REF TO cl_gui_container,
      g_container_2       TYPE REF TO cl_gui_container,
      go_pic              TYPE REF TO cl_gui_picture.
CONSTANTS:
  cns_extension TYPE i VALUE 3000.  "Docking size
DATA: len         TYPE i,
      url(256),
      resu        TYPE i VALUE 123,
      path_string TYPE string.

PARAMETERS: p_file LIKE ibipparms-path OBLIGATORY.
PARAMETERS: p_dis TYPE char1 AS CHECKBOX .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM select_file USING p_file.

START-OF-SELECTION.

  IF p_dis = ''.
    PERFORM upload_pic.
  ENDIF.

  PERFORM show_pic.

  CALL SCREEN 100.

FORM select_file USING p_file.

  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.

ENDFORM.

FORM upload_pic.
  path_string = p_file.
  DATA lv_content TYPE xstring.

* 上载PIC文件
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename   = path_string
      filetype   = 'BIN'
    IMPORTING
      filelength = len
    TABLES
      data_tab   = pic_tab[].

  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = len
    IMPORTING
      buffer       = lv_content
    TABLES
      binary_tab   = pic_tab[]
    EXCEPTIONS
      failed       = 1
      OTHERS       = 2.

  wa_pic-key1    = '主键字段'.
  wa_pic-field1 = lv_content.
  MODIFY zpictable FROM wa_pic.

  IF sy-subrc = 0 .
    MESSAGE '图片已经保存至数据库表' TYPE 'S'.
  ELSE.
  ENDIF.
ENDFORM.

FORM show_pic.
  CLEAR pic_tab.

  SELECT SINGLE *
    INTO wa_pic
    FROM zpictable
    WHERE key1 = '主键字段'.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = wa_pic-field1
    TABLES
      binary_tab = pic_tab.

* 创建图片URL
  CALL FUNCTION 'DP_CREATE_URL'
    EXPORTING
      type    = 'IMAGE'
      subtype = 'JPG'
    TABLES
      data    = pic_tab
    CHANGING
      url     = url.

  IF g_docking_container IS INITIAL."创建框体
    CREATE OBJECT g_docking_container
      EXPORTING
        style     = cl_gui_control=>ws_child
        repid     = sy-cprog
        dynnr     = '100'
        side      = g_docking_container->dock_at_left
        lifetime  = cl_gui_control=>lifetime_imode
        extension = cns_extension
      EXCEPTIONS
        OTHERS    = 1.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid
            TYPE sy-msgty
          NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDIF.

* SPLITTER CONTAINER
  IF g_splitter IS INITIAL. "动态分割一个框体
    CREATE OBJECT g_splitter
      EXPORTING
        parent  = g_docking_container
        rows    = 1
        columns = 1.

    c_pic  = g_splitter->get_container( row = 1 column = 1 ).

  ENDIF.
*
*  CREATE OBJECT c_pic
*    EXPORTING
*      container_name = 'C_PIC'.

  CREATE OBJECT go_pic
    EXPORTING
      parent = c_pic.

  CALL METHOD go_pic->load_picture_from_url
    EXPORTING
      url    = url
    IMPORTING
      result = resu.
* 3,居中显示
  CALL METHOD go_pic->set_display_mode
    EXPORTING
      display_mode = 3.

ENDFORM.

MODULE status_0100 OUTPUT.
  SET PF-STATUS '100'.
ENDMODULE.

MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK'.
      CALL METHOD c_pic->free.
*      (必须释放对象,否则会出现同一屏幕上操作图片无法刷新的情况)
      LEAVE TO SCREEN 0.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.

实现起来挺简单的,存储也比较方便。难度基本为0.大图片回比较慢,网速限制嘛。没有明显缺点。

外部图片URL地址显示

比较简单,可以用外部文件地址或者固定的服务器文件路径直接做图片显示。

代码语言:javascript复制
DATA picture_control_1 TYPE REF TO cl_gui_picture.
    CALL METHOD picture_control_1->set_registered_events
      EXPORTING
        events = event_tab.
            CREATE OBJECT event_receiver.“图片屏幕的事件
    SET HANDLER event_receiver->event_handler_picture_dblclick
                FOR picture_control_1.
    SET HANDLER event_receiver->event_handler_context_menu
                FOR picture_control_1.
    SET HANDLER event_receiver->event_handler_context_menu_sel
                FOR picture_control_1.
                
    CALL METHOD picture_control_1->set_display_mode
      EXPORTING
        display_mode = cl_gui_picture=>display_mode_normal.

    CALL METHOD picture_control_1->set_3d_border
      EXPORTING
        border = 1.

* new async implementation since 4.6C ”关联外部URL
    CALL FUNCTION 'DP_PUBLISH_WWW_URL'
      EXPORTING
        objid    = 'HTMLCNTL_TESTHTM2_SAP_AG'
        lifetime = cndp_lifetime_transaction
      IMPORTING
        url      = url
      EXCEPTIONS
        OTHERS   = 1.


    READ TABLE gt_alv WITH KEY icon = icon_green_light.
    IF sy-subrc EQ 0.
      url = gt_alv-zbpicurl.

      CALL METHOD picture_control_1->load_picture_from_url_async
        EXPORTING
          url = url.
    ENDIF.

配合分屏幕和OO ALV,可以实现左面是ALV,点击哪个显示哪个的图片这样的功能。

0 人点赞