ABAP 实现将上传文件转换为xstring,再将xstring转化为文件并打开,且解决乱码问题
时间: 2024-03-27 19:39:51 浏览: 298
ABAP从FTP下载文件并解析到内表移动文件位置.zip
5星 · 资源好评率100%
在ABAP中,将上传文件转换为xstring,再将xstring转化为文件并打开可以通过以下步骤实现:
1. 将上传文件转换为xstring。
```
DATA: lv_xstring TYPE xstring.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = cl_gui_frontend_services=>file_size( p_file )
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_binary.
```
2. 将xstring转换为文件并保存。
```
DATA: lv_file TYPE string.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = cl_gui_frontend_services=>file_size( lv_xstring )
filename = lv_file
filetype = 'BIN'
IMPORTING
filelength = lv_filesize
CHANGING
data_tab = lt_binary
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.
```
3. 打开文件。
```
CALL FUNCTION 'WS_EXECUTE'
EXPORTING
program = lv_file.
```
为了解决乱码问题,可以在打开文件前将xstring进行转换。例如:
```
DATA: lv_file TYPE string.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_xstring
IMPORTING
output_length = lv_filesize
TABLES
binary_tab = lt_binary.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_filesize
filename = lv_file
filetype = 'BIN'
IMPORTING
filelength = lv_filesize
CHANGING
data_tab = lt_binary
EXCEPTIONS
...
```
这样就可以避免文件内容乱码的问题了。
阅读全文