ABAP 上传图片
Sap图片上传程序:
1、 全局字段定义
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 ztpic.
data: c_pic type ref to cl_gui_custom_container,
pic type ref to cl_gui_picture.
data: len type i,url(256),
resu type i value 123,
path_string type string.
2、 选择屏幕定义
selection-screen begin of block blk1 with frame.
parameters : p_matnr like zmbt-matnr.
parameters : p_file like rlgrap-filename obligatory .
selection-screen end of block blk1.
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
*&---------------------------------------------------------------------*
form select_file using p_file.
call function 'F4_FILENAME'
importing
file_name = p_file.
endform. " SELECT_FILE
3、 图片上传及保存到数据库
*&---------------------------------------------------------------------*
*& Form UPLOAD_PIC
*&---------------------------------------------------------------------*
form upload_pic .
path_string = p_file.
data lv_content type xstring.
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-matnr = p_matnr.
wa_pic-picdata = lv_content.
modify ztpic from wa_pic.
if sy-subrc = 0.
message '图片已保存到数据库' type 'S'.
else.
message '图片保存失败!' type 'E'.
endif.
endform. " UPLOAD_PIC
4、 显示数据库中的图片
*&---------------------------------------------------------------------*
*& Form SHOW_PIC
*&---------------------------------------------------------------------*
form show_pic .
clear pic_tab.
select single * from ztpic into wa_pic where matnr = p_matnr.
call function 'SCMS_XSTRING_TO_BINARY'
exporting
buffer = wa_pic-picdata
tables
binary_tab = pic_tab.
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 pic
exporting
parent = c_pic.
call method pic->load_picture_from_url
exporting
url = url
importing
result = resu.
endform. " SHOW_PIC
5、 用到的数据库表:ZTPIC
6、 屏幕100:因为图片需要放在容器中显示,所以创建一个屏幕,屏幕中只有一个定制控制,名为C_PIC。以下是屏幕元素清单:
在程序中加入
data: ok_code like sy-ucomm,
save_ok like sy-ucomm.
屏幕GUI只需要一个退出功能即可
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0100 output.
set pf-status '100'.
endmodule. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0100 input.
save_ok = ok_code.
clear ok_code.
case save_ok.
when 'BACK'.
leave to screen 0.
endcase.
endmodule. " USER_COMMAND_0100 INPUT