20.26. 下載文件
20.26.1. 以 BIN 二進制下載
DATA : xstr TYPE xstring .
DATA : l_codepage ( 4 ) TYPE n .
DATA : l_encoding ( 20 ).
********** 字符集名與內碼轉換
" 將外部字符集名轉換為內部編碼
CALL FUNCTION ' SCP_CODEPAGE_BY_EXTERNAL_NAME '
EXPORTING
external_name = 'UTF-8'
IMPORTING
sap_codepage = l_codepage .
l_encoding = l_codepage .
********** 編碼
DATA : convout TYPE REF TO cl_abap_conv_out_ce .
" 創建編碼對象
convout = cl_abap_conv_out_ce => create ( encoding = l_encoding ).
convout -> write ( data = ' 江正軍 ' ). " 編碼
xstr = convout -> get_buffer ( ). " 獲取二進制碼流
WRITE : / xstr . "E6B19FE6ADA3E5869B
********** 解碼
DATA : convin TYPE REF TO cl_abap_conv_in_ce .
" 創建解碼對象
convin = cl_abap_conv_in_ce => create ( encoding = l_encoding input = xstr ).
DATA : str TYPE string .
CALL METHOD convin -> read " 解碼
IMPORTING data = str .
WRITE : / str . " 江正軍
TYPES : xx ( 100 ) TYPE x .
DATA : xtab TYPE STANDARD TABLE OF xx WITH HEADER LINE .
xtab = xstr .
APPEND xtab .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'c:\2.txt'
filetype = 'BIN'
TABLES
"data_tab 的類型為 ANY ,所以 xtab 是一列還是多列,都會寫到
" 文件中去,這里還只有一列,而且還沒有列名,這也沒有關系
data_tab = xtab[] .
20.26.2. 以字符模式下載
DATA : BEGIN OF strc OCCURS 0 ,
c1 ( 2 ) TYPE c ,
c2 ( 1 ) TYPE c ,
END OF strc .
strc - c1 = ' 中 ' .
strc - c2 = ' 國 ' .
APPEND strc .
APPEND strc .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = 'c:\1.txt'
filetype = 'DAT' " 列與列之間會使用 TAB 分隔
* APPEND = ' '
* WRITE_FIELD_SEPARATOR = ' '
* HEADER = '00'
* codepage = '8400' "GBK
* codepage = '8450' "GB2312
codepage = '4110' "utf-8
* CODEPAGE = '4102'"UTF-16BE
* CODEPAGE = '4103'"UTF-16LE
TABLES
data_tab = strc[] .
CODEPAGE
l Description
Use parameter CODEPAGE to specify the desired target codepage. If this parameter is not set, the codepage of the SAP GUI is used as the target codepage. 如果不指定,則使用 SAP GUI 所使用的 Codepage 。
l Value range
4-digit number of the SAP codepage. The function module SCP_CODEPAGE_BY_EXTERNAL_NAME returns the SAP codepage number for an external character set name, for example, "iso-8859-1". The function module NLS_GET_FRONTEND_CP returns the appropriate non-Unicode frontend codepage for a language.
You can determine the desired codepage interactively, if the parameter with_encoding of method file_save_dialog is set by cl_gui_frontend_services.
SPACE: Codepage of the SAP GUI
l Default
SPACE
SCP_CODEPAGE_BY_EXTERNAL_NAME
該函數可將字符集名稱轉換為 CODEPAGE ,也可以直接查看 TCP00A 表
另外,發現 TCP00 表里也存儲了 CODEPAGE ,而且該表有一個 CPCOMPANY 字段標示該代碼是由哪個組織定義的(一般我們使用 ISO 國際標准),可以將 TCP00A 與 TCP00 通過 CODEPAGE 聯合起來查詢, TCP00A 可以根據字符集名稱(如 GBK 、 UTF-8 ) TCP00A-CPATTR 來查詢,而 TCP00 可以根據字符集描述(如: Chinese )來查詢 TCP00- CPCOMMENT 。