將客戶端圖片儲存到資料庫中的方法
核心內容:
(1)使用到兩個函式模組(FM):SCMS_BINARY_TO_XSTRING 和 SCMS_XSTRING_TO_BINARY;
(2)資料庫儲存圖片的欄位設為 RAWSTRING型別(對應於ABAP資料型別XSTRING)。
一、假設場景及前提
(1)將客戶端C盤下的“PIC.JPG”檔案儲存到表ZTPIC中;
(2)ZTPIC表中包含一個名為PICDATA,型別為RAWSTRING的欄位用於儲存圖片內容;
(3)當需要顯示的時候,將此欄位的圖片在Picture控制元件中顯示出來。
以下是實現的程式碼:
(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)儲存(程式碼實現)
path_string = 'C:\PIC.jpg'.
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其他元件的賦值略!
wa_pic-picdata = lv_content.
insert into ztpic values wa_pic.
(3)顯示(程式碼實現)
clear pic_tab.
select single * from ztpic into wa_pic. “簡單起見,假設只有一條記錄
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.