如何利用 ABAP 进行文件的上传与下载

2023年 9月 22日 90.0k 0

通常,每当我们在ABAP中处理文件时,我们都会处理驻留在SAP NetWeaver AS ABAP主机上可访问的某个目录中的文件。但是,有时我们会遇到需要从 SAP GUI 表示层客户端上传或下载文件的情况。这种远程传输是使用 RFC 协议实现的。

类 CL_GUI_FRONTEND_SERVICES 提供了许多与 SAP GUI 前端接口的有用方法。下图包含一个 UML 类图,该类图显示了一些可用于处理前端客户端上的文件和目录的方法。您可以在类生成器中可用于此类的类文档中找到有关这些方法的详细信息。

image.png

下载文件

若要下载文件,请使用方法 CL_GUI_FRONTEND_SERVICES⇒GUI_DOWNLOAD

CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
      FILENAME                  = FULLPATH
      FILETYPE                  = 'DAT'
    CHANGING
      DATA_TAB                  = itab

您还可以使用功能模块 GUI_DOWNLOAD:

CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      bin_filesize            = l_filesize
      filename                = l_filename
      filetype                = 'BIN'
    TABLES
      data_tab                = ltbl_pdf
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      OTHERS                  = 22.
  IF sy-subrc  0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

文件上传

若要上传文件,请使用方法 CL_GUI_FRONTEND_SERVICES⇒GUI_UPLOAD

CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
    EXPORTING
      FILENAME                  = FULLPATH
      FILETYPE                  = 'DAT'
    CHANGING
      DATA_TAB                  = itab

例如:

*DATA DECLARATION for GUI_DOWNLOAD.
 DATA : lv_filename type STRING.
lv_filename = file.
CONSTANTS: LV_FILETYPE TYPE CHAR10 VALUE 'ASC'.
 CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME                        = lv_filename
    FILETYPE                        = 'ASC'
    CODEPAGE                        = 'IBM'
  TABLES
    DATA_TAB                        = DATA_TAB
  EXCEPTIONS
    FILE_WRITE_ERROR                = 1
    NO_BATCH                        = 2
    GUI_REFUSE_FILETRANSFER         = 3
    INVALID_TYPE                    = 4
    NO_AUTHORITY                    = 5
    UNKNOWN_ERROR                   = 6
    HEADER_NOT_ALLOWED              = 7
    SEPARATOR_NOT_ALLOWED           = 8
    FILESIZE_NOT_ALLOWED            = 9
    HEADER_TOO_LONG                 = 10
    DP_ERROR_CREATE                 = 11
    DP_ERROR_SEND                   = 12
    DP_ERROR_WRITE                  = 13
    UNKNOWN_DP_ERROR                = 14
    ACCESS_DENIED                   = 15
    DP_OUT_OF_MEMORY                = 16
    DISK_FULL                       = 17
    DP_TIMEOUT                      = 18
    FILE_NOT_FOUND                  = 19
    DATAPROVIDER_EXCEPTION          = 20
    CONTROL_FLUSH_ERROR             = 21
    OTHERS                          = 22.
file = lv_filename.

您也可以使用功能模块 GUI_UPLOAD

DATA : lv_filename type STRING.

lv_filename = file.

CONSTANTS: LV_FILETYPE TYPE CHAR10 VALUE 'ASC'.

CALL FUNCTION 'GUI_UPLOAD'
	EXPORTING
       FILENAME              = lv_filename
       FILETYPE              = 'DAT'
       CODEPAGE              = 'IBM'
    TABLES
       DATA_TAB             = data_tab
	EXCEPTIONS
       FILE_OPEN_ERROR               = 1
       FILE_READ_ERROR               = 2
       NO_BATCH                      = 3
       GUI_REFUSE_FILETRANSFER       = 4
       INVALID_TYPE                  = 5
       NO_AUTHORITY                  = 6
       UNKNOWN_ERROR                 = 7
       BAD_DATA_FORMAT               = 8
       HEADER_NOT_ALLOWED            = 9
       SEPARATOR_NOT_ALLOWED         = 10
       HEADER_TOO_LONG               = 11
       UNKNOWN_DP_ERROR              = 12
       ACCESS_DENIED                 = 13
       DP_OUT_OF_MEMORY              = 14
       DISK_FULL                     = 15
       DP_TIMEOUT                    = 16
       OTHERS                        = 17.
file = lv_filename.

打开对话框显示

方法:CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

功能模块:WS_FILENAME_GET 或者 F4_FILENAME

保存对话框显示

若要显示“保存文件”对话框,请使用方法 CL_GUI_FRONTEND_SERVICES⇒FILE_SAVE_DIALOG

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
    CHANGING
      FILENAME             = FILENAME
      PATH                 = PATH
      FULLPATH             = FULLPATH
    EXCEPTIONS
      CNTL_ERROR           = 1
      ERROR_NO_GUI         = 2
      NOT_SUPPORTED_BY_GUI = 3
      OTHERS               = 4

复制到剪贴板

若要将数据复制到剪贴板,请使用方法 cl_gui_frontend_services⇒clipboard_export

DATA lit_char10 TYPE STANDARD TABLE OF char10 .
APPEND 'ABCDEFG' TO lit_char10.
DATA l_return TYPE i.

CALL METHOD cl_gui_frontend_services=>clipboard_export
  IMPORTING
    data = lit_char10
  CHANGING
    rc   = l_return.

检查文件是否存在

  • 功能模块:DX_FILE_EXISTENCE_CHECK

  • 方法:CL_GUI_FRONTEND_SERVICES=>FILE_EXIST

选择目录

方法:CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论