Tuesday, October 25, 2016

Files Upload/Download to application server/local

I. Upload File
1. OPEN DATASET
Open dataset statement is used to work with a file on application server.
OPEN DATASET dset
 FOR access
     IN mode [position]
                    [os_additions]
                    [error_handling]
     ENCODING ...

Access:
... INPUT
The addition FOR INPUT opens the file for reading. By default, the file pointer is set at the start of the file. If the file specified does not exist, sy-subrc is set to 8.

In a Unicode program, it is not possible to obtain write access to a file that is open for reading. In a non-Unicode program write access is also permitted.

... OUTPUT
The addition FOR OUTPUT opens the file for writing. If the specified file already exists, its content is deleted. If the file specified does not exist, it is created.

Data is read or written unchanged (as stored in the memory). (For details, see READ DATASET and TRANSFER.)

Mode:
TEXT MODE encoding [linefeed]
The addition IN TEXT MODE opens the file as a text file. Only the content of character-like data objects can be passed to text files and read from text files.

The addition ENCODING defines how the characters are represented in the text file. When writing to a text file, the content of a data object is converted to the representation entered after ENCODING, and passed to the file.

The end-of-line selection of the relevant platform is applied to the passed data by default. When reading from a text file, the content of the file is read until the next end-of-line selection, converted from the format specified after ENCODING into the current character format, and passed to a data object. The end-of-line selection used is controlled using the addition linefeed.
E.g.
open dataset lv_filename for output in text mode encoding utf-8 with byte-order mark with windows linefeed.

LEGACY TEXT MODE [endian] [CODE PAGE cp] [linefeed]
By specifying LEGACY, files can be written in the format that is expected by a non-Unicode system. The byte order (BOM) or the code page (look at TCP00A table) must be specified explicitly.

ENCODING
It defines the character representation in which the content of a text file is handled.
It is best to always write files in UTF-8 (if all readers can process this format). Otherwise, the code page can depend on the text environment and it is difficult to identify the code page from the file content.

TRANSFER LW_LINE TO P_EFILE.
Transfer a line to a file on application server.

This open dataset can be used to input, output text file, pdf file (smartform), ... to application server.

2. Upload File from PC to Server
CALL FUNCTION 'GUI_UPLOAD'
   EXPORTING
     FILENAME           = W_FNAME       "File path
     FILETYPE            = 'ASC'               " File type as ASCII
     HAS_FIELD_SEPARATOR = 'X'       "Fields are separated by tabs
   TABLES
     DATA_TAB            = TD_INFILE
   EXCEPTIONS
     OTHERS               = 0.

The module loads a file from the PC to the server.
Data can be transferred binarily or as text (file type = BIN/ASC/DAT).

The uploaded file is usually in text format with extension .txt and have tab seperator. In case of uploading a csv file, we can read each entire row to a string table (TABLE OF STRING), then use command SPLIT to get each field from the comma seperator.

For uploading an excel file, we can use the FM 'ALSM_EXCEL_TO_INTERNAL_TABLE', ref upload-excel-to-sap

II. Download a file
1. Download File from Server to PC
This function module downloads a file from server to PC.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    FILENAME                                        = lw_fname
    FILETYPE                                         = 'ASC'
    WRITE_FIELD_SEPARATOR                = 'X'
    TRUNC_TRAILING_BLANKS                = 'X'
  TABLES
    DATA_TAB                                        = t_file
  .

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...