写点什么

ABAP 文件上 / 下载

作者:Jasen Ye
  • 2022 年 3 月 25 日
  • 本文字数:3172 字

    阅读完需:约 10 分钟

ABAP 文件上/下载

SAP 提供的文件上/下载方式有:GUI_UPLOAD && GUI_DOWNLOAD

以及使用更为简洁的: CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD(...)

CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD(...)


1. 模板下载(SMW0 的模板文件)


  METHOD download_template.****************************************************************************示例:***     AT SELECTION-SCREEN.***      CASE sscrfields-ucomm.***        WHEN 'FC01'.***          zcl_devp_tool=>get_instance( )->download_template( EXPORTING im_filename = 'ZMM056' im_init_filename = '库存状态调整单批导模板' ).***      ENDCASE.*************************************************************************
DATA:lo_objdata TYPE wwwdatatab, lo_mime TYPE w3mime, ls_destination TYPE rlgrap-filename, ls_objnam TYPE string, li_rc TYPE sy-subrc, ls_errtxt TYPE string. DATA:p_objid TYPE wwwdatatab-objid, p_dest TYPE sapb-sappfad. DATA:l_path TYPE string, l_fullpath TYPE string, l_filename TYPE string, gc_fullpath TYPE rlgrap-filename. DATA:lv_init_directory TYPE string, lv_init_filename TYPE string.
lv_init_directory = 'C:/' && im_init_directory. lv_init_filename = im_init_filename && '.xlsx'.
CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING window_title = 'Select directory' initial_directory = lv_init_directory "'C:/' default_file_name = lv_init_filename file_filter = '.xlsx' CHANGING filename = l_filename path = l_path fullpath = l_fullpath EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4.
p_objid = im_filename.
CHECK l_fullpath IS NOT INITIAL.
IF sy-subrc = 0.
CONCATENATE p_objid '.XLS' INTO ls_objnam.
CONDENSE ls_objnam NO-GAPS.
SELECT SINGLE relid objid INTO CORRESPONDING FIELDS OF lo_objdata FROM wwwdata WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid.
IF sy-subrc NE 0 OR lo_objdata-objid EQ space. MESSAGE '模板不存在于系统' TYPE 'W'. EXIT. ENDIF.
CONCATENATE l_fullpath '.xls' INTO gc_fullpath.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT' EXPORTING key = lo_objdata destination = gc_fullpath IMPORTING rc = li_rc.
IF li_rc NE 0. MESSAGE '模板下载失败!' TYPE 'W'. ENDIF.
ENDIF.

ENDMETHOD.
复制代码


  1. 图片上载(针对于 SE78 图形)


METHOD upload_graphics.
DATA:cv_docid TYPE bds_docid, cv_filepath TYPE localfile, cv_resolution TYPE tdresolut.
cv_filepath = get_file_address( ).
PERFORM import_bitmap_bds IN PROGRAM saplstxbitmaps USING cv_filepath "文件路径 i_tdname "名字 i_tdobject "对象 固定为 GRAPHIC i_tdid "ID 固定为 BMAP i_tdbtype "颜色模式 BMON黑白 BCOL彩色 i_format "BMP 格式 i_description "描述 i_resident "依照打印请求驻留在打印机内存中 X 驻留 i_autoheight "自动高度 i_bmcomp "是否压缩 CHANGING cv_docid "文档ID cv_resolution.
SELECT SINGLE docid FROM stxbitmaps INTO cv_docid WHERE tdobject = i_tdobject AND tdname = i_tdname AND tdid = i_tdid AND tdbtype = i_tdbtype AND docid = cv_docid. IF sy-subrc = 0. o_return-type = 'S'. ELSE. o_return-type = 'E'. ENDIF.

ENDMETHOD. -----------------示例------------------"上载se78彩色图片*zcl_devp_tools=>get_instance( )->upload_graphics( EXPORTING i_tdname = 'ZSS_LOGO' i_description = '测试上传图片' ).
复制代码


3.图片上载(针对于 SE78 图形)


  METHOD download_graphics.
DATA cv_path TYPE string. DATA:v_graphic_size TYPE i, v_graphic_xstr TYPE xstring, v_graphic_conv TYPE i, v_graphic_offs TYPE i, v_file TYPE string. DATA: BEGIN OF cs_graphic_table, line(255) TYPE x, END OF cs_graphic_table. DATA ct_graphic_table LIKE TABLE OF cs_graphic_table.
cv_path = get_folder_path( ). CHECK cv_path IS NOT INITIAL.
* Get the bmp image from BDS in hex string format CALL METHOD cl_ssf_xsf_utilities=>get_bds_graphic_as_bmp EXPORTING p_object = i_tdobject p_name = i_tdname p_id = i_tdid p_btype = i_tdbtype RECEIVING p_bmp = v_graphic_xstr EXCEPTIONS not_found = 1 internal_error = 2 OTHERS = 3. IF sy-subrc = 0.* Find the length of hex string v_graphic_size = xstrlen( v_graphic_xstr ). CHECK v_graphic_size > 0.
v_graphic_conv = v_graphic_size. v_graphic_offs = 0.* Populate internal table from this hex string CLEAR ct_graphic_table. WHILE v_graphic_conv > 255. cs_graphic_table-line = v_graphic_xstr+v_graphic_offs(255). APPEND cs_graphic_table TO ct_graphic_table. v_graphic_offs = v_graphic_offs + 255. v_graphic_conv = v_graphic_conv - 255. ENDWHILE.
cs_graphic_table-line = v_graphic_xstr+v_graphic_offs(v_graphic_conv). APPEND cs_graphic_table TO ct_graphic_table.
* Prepare file name and file path CONCATENATE cv_path '\' i_tdname '.BMP' INTO cv_path .* Download image o_return = gui_download( EXPORTING i_bin_filesize = v_graphic_size i_filename = cv_path it_data_tab = ct_graphic_table ). ENDIF. ENDMETHOD. ---------------示例---------------"下载se78彩色图片*zcl_devp_tools=>get_instance( )->download_graphics( EXPORTING i_tdname = 'SS_LOGO' ).
复制代码


发布于: 刚刚阅读数: 2
用户头像

Jasen Ye

关注

还未添加个人签名 2022.03.23 加入

还未添加个人简介

评论

发布
暂无评论
ABAP 文件上/下载_upload_Jasen Ye_InfoQ写作平台