Google Search - Blog...........

ABAP - Uploading / Downloading Files From / To Application / Presentation Server.

This Code Snippets will clarify some general process of working with files when ABAP programmming. Such as,Uploading/Downloading files from/to Application/Presentation server, adding F4 help to file path on the selection-screen,checking file existence.
Check File Existence
Before we process the file which is input on the selection screen by end-user, we need to check whether this file exists or not. If it does not exist, we just give an error message and need not to go into the main program .This logic should be done within the event 'AT SELECTION-SCREEN'.
presentation server file
For the presentation server file,here intruduce 2 ways, one is Function Module, the other is Class Static Method.

1. Using Funciton Moudle 'DX_FILE_EXISTENCE_CHECK'.

DATA: gdf_file_exist(1) TYPE c.
PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN.
CALL FUNCTION 'DX_FILE_EXISTENCE_CHECK'
EXPORTING
filename = p_file
pc = 'X'
* SERVER =
IMPORTING
file_exists = gdf_file_exist

IF NOT ( sy-subrc = 0 and gdf_file_exist = 'X' )
MESSAGE 'the input file does not exist.' TYPE 'E'.
ENDIF.
Pay attention to the importing parameter 'pc', it should be set as 'X'.

2. Using Class Static Method 'CL_GUI_FRONTEND_SERVICES=>FILE_EXIST'
DATA: gdf_file_exist(1) TYPE c.
PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = p_file
RECEIVING
result = gdf_file_exist

IF NOT ( sy-subrc = 0 and gdf_file_exist = 'X' )
MESSAGE 'the input file does not exist.' TYPE 'E'.
ENDIF.
application server file
For application server file, generally we open it first within the event 'AT SELECTION-SCREEN'.If it can be opened successfully, this file exists. After open, do not forget to close it.
PARAMETERS p_file TYPE dxfile-filename.
AT SELECTION-SCREEN.
OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc = 0.
CLOSE DATASET p_file.
ELSE.
MESSAGE 'the input file does not exist.' TYPE 'E'.
ENDIF.
There is also a Function Module can be used, named 'OCS_GET_FILE_INFO'.

DATA: gdt_ocs_file TYPE TABLE OF ocs_file.
PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN.
CALL FUNCTION 'OCS_GET_FILE_INFO'
EXPORTING
dir_name = p_file
file_name = '*'
TABLES
dir_list = gdt_ocs_file
Importing parameter 'file_name' is set as '*' means all the file in the specfied directory will be get and stored in the internal table 'gdt_ocs_file'. If the input file is included in the internal table, this file exists.
Add F4 Help
Adding one F4 Help to the file path on the selection-screen will be very helpful to the end-user.The logic should be under the event 'ON VALUE-REQUEST'.
presentation server file
For the presentation server file,here intruduce 2 ways, one is Function Module, the other is Class Static Method.
1. Using Funciton Module 'F4_FILENAME'.
PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'
* EXPORTING
* PROGRAM_NAME =
* DYNPRO_NUMBER =
* FIELD_NAME =
IMPORTING
file_name = p_file.
2. Using Class Static Method 'CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG'.

DATA: gdt_filetable TYPE filetable.
DATA: gdf_rc TYPE I.
PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
WINDOW_TITLE = 'Choose a file'
CHANGING
file_table = gdt_filetable
rc = gdf_rc
IF sy-subrc = 0.
READ TABLE gdt_filetable
INTO gds_filetable INDEX 1.
p_file = gds_filetable-filename.
ENDIF.
gdf_rc is the number of selected file, if it is equal -1, error occured.
application server file
Generally it need not to provide F4 help for application server file. If we want it, there is also a Funciotn Module which can be used.

PARAMETERS p_file TYPE dxfile-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_DXFILENAME_4_DYNP'
EXPORTING
dynpfield_filename = 'P_FILE'
dyname = sy-repid
dynumb = sy-dynnr
filetype = 'P'
location = 'A'
server = ' '.
filetype: 'P' represents Physical file name; 'L' represents Logical file name.
location: 'A' represents Application Server; 'P' represents Presentation server.

Download Files
Sometimes we need to save the internal table data as a file for the further process.For download to presentation server and download to application server,they are 2 different kinds of process methods.
to presentation server
When storing internal table data as a local file on the presentation server, there are 2 methods we can use.
One is using the function modeule, the other is using the class static method.
1. Using Function Module 'GUI_DOWNLOAD'.

DATA: gdf_filepath type dxfile-filename
DATA: gdt_data type table of gts_data.

START-OF-SELECTION.

gdf_filepath = 'C:\mydata.txt'.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = gdf_filepath
filetype = 'ASC'
write_field_separator = 'X'
TABLES
data_tab = gdt_data.
filetype is set by 'X' mens separating columns by Tabs in case of ASCII download.
If sy-subrc is equal 0, file downloading is successful.

2. Using class static method 'CL_GUI_FRONTEND_SERVICES =>GUI_DOWNLOAD'

DATA: gdf_filepath type dxfile-filename
DATA: gdt_data type table of gts_data.

START-OF-SELECTION.

gdf_filepath = 'C:\mydata.txt'.

CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = gdf_filepath
filetype = 'ASC'
write_field_separator = 'X'
CHANGING
data_tab = gdt_data
filetype is set by 'X' mens separating columns by Tabs in case of ASCII download.
If sy-subrc is equal 0, file downloading is successful.
to application server
If we want to save the internal table data to the application server, there is no function module or class static method which we can use, we must wirte the code by ourselves.

DATA: gdf_filepath type dxfile-filename
DATA: gdt_data type table of gts_data.
DATA: ldf_length type i.
FIELD-SYMBOLS: TYPE gts_data

START-OF-SELECTION.

gdf_filepath = 'C:\mydata.txt'.

OPEN DATASET gdf_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT prt_data ASSIGNING .
DESCRIBE FIELD LENGTH ldf_length IN BYTE MODE.

TRANSFER TO prf_file LENGTH ldf_length.
ENDLOOP.

CLOSE DATASET gdf_filepath.
The prerequsite is the field of prt_data must be character type.
Using this method, every field column will output as the length defined,without separator.

If we want field columns are separated by tab, we can realize it as below.

DATA: gdf_filepath type dxfile-filename
DATA: gdt_data type table of gts_data.
DATA: ldf_length type i.
FIELD-SYMBOLS: TYPE gts_data

START-OF-SELECTION.

gdf_filepath = 'C:\mydata.txt'.

OPEN DATASET gdf_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT prt_data ASSIGNING .
CONCATENATE -BUKRS
-BUDAT
...
INTO LDF_OUTDATA
SEPARATED BY
CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

TRANSFER LDF_OUTDATA TO gdf_filepath.
ENDLOOP.

CLOSE DATASET gdf_filepath.
Upload Files
Sometimes we need to upload the file data to the internal table first, and then process it.For upload from presentation server and upload from application server,they are 2 different kinds of process methods.
from presentation server
When we upload a file data from presentation server to the internal table, there are 2 method that we can choose.One is using the Function Moduel, the other is using the class static method.

1. Using function module 'GUI_UPLOAD'.

DATA: gdt_filedata TYPE TABLE OF gts_filedata.
PARAMETERS p_file TYPE dxfile-filename.

START-OF-SELECTION.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = p_file
has_field_separator = 'X'
TABLES
data_tab = gdt_filedata
parameter has_field_separator is set as 'X' means columns separated by tabs in case of ASCII upload.

2. Using the class tatic method 'CL_GUI_FRONTEND_SERVICES =>GUI_UPLOAD'.

DATA: gdt_filedata TYPE TABLE OF gts_filedata.
PARAMETERS p_file TYPE dxfile-filename.

START-OF-SELECTION.

CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = p_file
has_field_separator = 'X'
CHANGING
data_tab = prt_table.
parameter 'has_field_separator' is set as 'X' means columns separated by tabs in case of ASCII upload.
from application server
If we want to upload file data from the application server to the internal table, there is no function module or class static method which we can use, we must wirte the code by ourselves.

1. For the file data which has no seperator between field columns.

PARAMETERS p_file TYPE dxfile-filename.

START-OF-SELECTION.

OPEN DATASET p_file IN TEXT MODE ENCODING DEFAULT FOR INPUT.

DO.
READ DATASET p_file INTO gds_data.
IF sy-subrc <> 0.
EXIT.
ENDIF.

APPEND gds_data TO gdt_data.
ENDDO.

CLOSE DATASET p_file.
2. For the file data which has tab separator between field columns.

DATA: gds_field_split type gts_data.
FIELD-SYMBOLS: TYPE gts_data.
PARAMETERS p_file TYPE dxfile-filename.

START-OF-SELECTION.

OPEN DATASET prf_file IN TEXT MODE ENCODING DEFAULT FOR INPUT.

DO.
READ DATASET p_file INTO gds_field.
SPLIT gds_field AT cl_abap_char_utilities=>horizontal_tab
INTO TABLE gdt_field_split.

LOOP AT gdt_field_split into gds_field_split.
gdf_index = gdf_index + 1.
ASSIGN COMPONENT gdf_index OF STRUCTURE
gds_data to .

IF sy-subrc = 0.
= gds_field_split.
ENDIF.
ENDLOOP.

APPEND gds_data TO gdt_data.
ENDDO.


CLOSE DATASET p_file.

No comments:

Post a Comment