1, 简介
整理了一段代码,实现上载本地图片到SAP的数据库表中,并且显示在屏幕上。
其中用到了一下函数:
1, GUI_UPLOAD:上载图片文件
2, SCMS_BINARY_TO_XSTRING:将图片文件转换成xstring
3, SCMS_XSTRING_TO_BINARY:将图片xstring转换成binary格式保存到内表中
4, DP_CREATE_URL:创建得到图片的URL
5, go_pic->load_picture_from_url:根据图片URL导入
6, go_pic->set_display_mode:显示图片
2, 完整代码
REPORT ztest_upload_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: c_pic TYPE REF TO cl_gui_custom_container, go_pic TYPE REF TO cl_gui_picture. DATA: len TYPE i, url(256), resu TYPE i VALUE 123, path_string TYPE string. PARAMETERS: p_file LIKE ibipparms-path OBLIGATORY. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM select_file USING p_file. START-OF-SELECTION. PERFORM upload_pic. 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. 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.
屏幕100:
PROCESS BEFORE OUTPUT. MODULE status_0100. * PROCESS AFTER INPUT. MODULE user_command_0100.
在屏幕上创建一个自定义的container:C_PIC
状态status 100,简单的就设置个BACK按钮
SE11 创建一个数据库表ZPICTABLE用来保存上载的图片文件,其中field1就是用来保存图片rawstring的字段。
3, 程序运行
选择画面输入图片文件路径,然后运行即可
然后美女就出现了。。。
以上。