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

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

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

下载文件

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

ABAP 复制代码
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
      FILENAME                  = FULLPATH
      FILETYPE                  = 'DAT'
    CHANGING
      DATA_TAB                  = itab

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

ABAP 复制代码
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

ABAP 复制代码
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD
    EXPORTING
      FILENAME                  = FULLPATH
      FILETYPE                  = 'DAT'
    CHANGING
      DATA_TAB                  = itab

例如:

ABAP 复制代码
*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

ABAP 复制代码
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

ABAP 复制代码
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

ABAP 复制代码
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

相关推荐
菜根Sec11 分钟前
XSS跨站脚本攻击漏洞练习
前端·xss
m0_7482571817 分钟前
Spring Boot FileUpLoad and Interceptor(文件上传和拦截器,Web入门知识)
前端·spring boot·后端
桃园码工35 分钟前
15_HTML5 表单属性 --[HTML5 API 学习之旅]
前端·html5·表单属性
百万蹄蹄向前冲1 小时前
2024不一样的VUE3期末考查
前端·javascript·程序员
轻口味2 小时前
【每日学点鸿蒙知识】AVCodec、SmartPerf工具、web组件加载、监听键盘的显示隐藏、Asset Store Kit
前端·华为·harmonyos
alikami2 小时前
【若依】用 post 请求传 json 格式的数据下载文件
前端·javascript·json
吃杠碰小鸡2 小时前
lodash常用函数
前端·javascript
emoji1111112 小时前
前端对页面数据进行缓存
开发语言·前端·javascript
泰伦闲鱼2 小时前
nestjs:GET REQUEST 缓存问题
服务器·前端·缓存·node.js·nestjs
m0_748250033 小时前
Web 第一次作业 初探html 使用VSCode工具开发
前端·html