Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CODE FOR SENDING AND RECEVING FILE FROM APPLICATION SERVER

Former Member
0 Likes
1,251

WHAT IS THE ABAP CODE FOR SENDING A FILE TO APPLICATION SERVER AND RECEVING SAME FILE FROM APPLICATION SERVER

WHAT IS THE ABAP CODE FOR SENDING A FILE TO APPLICATION SERVER AND RECEVING SAME FILE FROM APPLICATION SERVER

6 REPLIES 6
Read only

Former Member
0 Likes
1,095

For reading from application server :


OPEN DATASET p_file IN TEXT MODE FOR INPUT ENCODING DEFAULT.

 READ DATASET p_file INTO l_str.

 CLOSE DATASET p_file.

p_file is application server file path.and l_str is a local string.

For writting into Application server :



 OPEN DATASET p_ofile_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

      LOOP AT p_local_table INTO wa_local.
        TRANSFER wa_local TO p_ofile_file.
        CLEAR: wa_local.
      ENDLOOP.

      CLOSE DATASET p_ofile_file.

You can also use the T-Code cg3y to download data from application and T-Code cg3z to upload file to application server.

Read only

Sandeep_Panghal
Product and Topic Expert
Product and Topic Expert
0 Likes
1,095

Hi,

First tell your Basis person to create a DIRECTORY on application server and after that place your file in that folder.

The content of that file you can access in your ZREPORT using commands

OPEN DATASET --> to open the file

READ DATASET '/usr/test.dat' INTO rec --> to read the data .

CLOSE DATASET --> close the file.

*Reward if helps

Regards,

Sandeep

Read only

Former Member
0 Likes
1,095

Hi,

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(12) VALUE 'abcdefghijkl',

TEXT2(5),

LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE.

TRANSFER TEXT1 TO FNAME.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

DO.

READ DATASET FNAME INTO TEXT2 LENGTH LENG.

WRITE: / SY-SUBRC, TEXT2, LENG.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET FNAME.

Note :give proper file from TCODE "AL11"

Read only

Former Member
0 Likes
1,095

Hi,

You can use the DATASET in output & input mode to read & write data from & to application server.

For writing data to application server:

  • APPLICATION FILE CREATION.

  • Check whether file already exists vkont

OPEN DATASET c_filepath_fpb3 "/tmp/file_FI009_01_fpb3.txt

FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

  • File c_filepath_fpb3 already exists then delete that file

DELETE DATASET c_filepath_fpb3. "/tmp/file_FI009_01_fpb3.txt

ENDIF.

  • Open the filepath / dataset on the application server for writing

OPEN DATASET c_filepath_fpb3 "/tmp/file_FI009_01_fpb3.txt

FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  • Check if the OPEN has been successful

IF sy-subrc EQ 0.

  • Transfer file header data

TRANSFER wa_bfkkzgr00 TO c_filepath_fpb3.

"/tmp/file_FI009_01_fpb3.txt

CLEAR: wa_bfkkzgr00.

  • Transfer paymentlot header data

TRANSFER wa_bfkkzk TO c_filepath_fpb3.

"/tmp/file_FI009_01_fpb3.txt

CLEAR: wa_bfkkzk.

LOOP AT it_bfkkzp INTO wa_bfkkzp.

TRANSFER wa_bfkkzp TO c_filepath_fpb3.

"/tmp/file_FI009_01_fpb3.txt

CLEAR: wa_bfkkzp.

ENDLOOP.

  • Close the dataset / filepath

CLOSE DATASET c_filepath_fpb3.

"/tmp/file_FI009_01_fpb3.txt

ENDIF.

  • Call the report for creating the payment lots(Transaction FPB3)

  • Submit the data to park the document

SUBMIT rfkkze00 WITH as_fname = c_filepath_fpb3

"/tmp/file_FI009_01_fpb3.txt

AND RETURN.

ENDIF.

For Reading data from application server:

          • Getting values in internal table from file on appication server

  • Uploading the file

OPEN DATASET v_app_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

WHILE sy-subrc EQ 0.

CLEAR wa_part_upload.

READ DATASET v_file INTO wa_part_upload.

APPEND wa_part_upload TO it_part_upload.

ENDWHILE.

CLOSE DATASET v_app_file.

Please reward, if its helpful.

Thanks,

Abhishek.

Read only

Former Member
0 Likes
1,095

Hi,

Get the input file path and give it in p_inpfile

OPEN DATASET p_inpfile FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

WRITE 😕 text-004. "'No such input file' COLOR 3.

EXIT.

ELSE.

DO.

CLEAR : wa_string,wa_0001.

*Reading the file from application server and moving to work area*

READ DATASET p_inpfile INTO wa_string.

IF sy-subrc NE 0.

EXIT.

ELSE.

CLEAR wa_0001.

*Spliting fields in the file-

REPLACE ALL OCCURRENCES OF '#' IN wa_string WITH ' '.

SPLIT wa_string AT c_htab INTO wa_0001-pernr

wa_0001-werks "Personnel Area

wa_0001-persk "Employee Sub-Group

wa_0001-vdsk1 "Org. Key

wa_0001-abkrs "Payroll area

wa_0001-ansvh. "Work contract

wa_0001-begda = p_efdate.

wa_0001-endda = '99991231'.

APPEND wa_0001 TO int_0001.

ENDIF.

ENDDO.

CLOSE DATASET p_inpfile.

The bigger size fonts are internal table fields which are appending values from input file and from this input file we upload data to application server.

Thanks,

Sakthi C

Read only

Former Member
0 Likes
1,095

HAI Bharat,

open a file on the application server, use the OPEN statement as follows:

Syntax

OPEN DATASET <dsn> [Additions].

This statement opens the file <dsn>. If you do not specify any additions for the mode, the file is opened in binary mode for reading. SY-SUBRC returns 0 if the system opens the file. Otherwise, SY-SUBRC is set to 8.

You enter the filename <dsn> either as a literal or as a field containing the actual name of the file. If you do not specify a path, the system opens the file in the directory in which the R/3 System is running on the application server. To open a file, the user under which the R/3 System is running must have the requisite authorizations at operating system level.

Filenames are platform-specific. You must therefore use file- and pathnames that conform to the rules of the operating system under which your R/3 System is running. However, you can also use logical filenames to ensure that your programs are not operating system-specific. For further information, refer to Using Platform-Independent Filenames.

DATA FNAME(60).

FNAME = '/tmp/myfile'.

OPEN DATASET 'myfile'.

OPEN DATASET FNAME.

This example works as long as your R/3 System is running under UNIX. The program opens the file "myfile" in the directory in which the R/3 System is running, and also opens the file "myfile" in directory "/tmp". However, you would have to change the filename for other operating systems. For example, for OpenVMS, you could write the following:

FNAME = '[TMP]myfile.BIN'

OPEN DATASET 'myfile.BIN'.

To open a file for reading, use the FOR INPUT addition to the OPEN DATASET statement.

Syntax

OPEN DATASET <dsn> FOR INPUT.

The file must already exist, otherwise, the system sets SY-SUBRC to 8, and ignores the statement.

If the file exists and is already open (for read or write access, or for appending), the position is reset to the beginning of the file. However, it is good programming style to close files that are already open before you reopen them for a different operation (for further information about closing files, refer to Closing a File).

DATA FNAME(60) VALUE 'myfile'.

OPEN DATASET FNAME FOR INPUT.

IF SY-SUBRC = 0.

WRITE / 'File opened'.

.....

ELSE.

WRITE / 'File not found'.

ENDIF.

This example opens the file "myfile" for reading.

open a file for writing, use the FOR OUTPUT addition to the OPEN DATASET statement.

Syntax

OPEN DATASET <dsn> FOR OUTPUT.

If the file does not already exist, it is created automatically. If it does already exist, but is closed, its contents are overwritten. If the file exists and is already open (for read or write access, or for appending), the position is reset to the beginning of the file. If the system can open the file <dsn> successfully, SY-SUBRC is set to 0. If not, it is set to 8.

DATA: MESS(60),

FNAME(10) VALUE '/tmp'.

OPEN DATASET FNAME FOR OUTPUT MESSAGE MESS.

IF SY-SUBRC <> 0.

WRITE: 'SY-SUBRC:', SY-SUBRC,

/ 'System Message:', MESS.

ENDIF.

If the R/3 System is ruining under UNIX, the output looks like this:

The system cannot open the file, since the name you specified is that of a directory.

The following program shows how the system sets the position when you open a file for writing. However, it is better programming style to close files that are already open before you reopen them for a different operation (for further information about closing files, refer to Closing a File).

DATA FNAME(60) VALUE 'myfile'.

DATA NUM TYPE I.

OPEN DATASET FNAME FOR OUTPUT.

DO 10 TIMES.

NUM = NUM + 1.

TRANSFER NUM TO FNAME.

ENDDO.

PERFORM INPUT.

OPEN DATASET FNAME FOR OUTPUT.

NUM = 0.

DO 5 TIMES.

NUM = NUM + 10.

TRANSFER NUM TO FNAME.

ENDDO.

PERFORM INPUT.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR OUTPUT.

NUM = 0.

DO 5 TIMES.

NUM = NUM + 20.

TRANSFER NUM TO FNAME.

ENDDO.

PERFORM INPUT.

FORM INPUT.

SKIP.

OPEN DATASET FNAME FOR INPUT.

DO.

READ DATASET FNAME INTO NUM.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

WRITE / NUM.

ENDDO.

ENDFORM.

The output appears as follows:

1

2

3

4

5

6

7

8

9

10

10

20

30

40

50

6

7

8

9

10

20

40

60

80

100

This example performs the following steps using the file "myfile":

It is opened for writing

It is filled with 10 integers (for information about the TRANSFER statement, refer to Writing Data to Files)

It is then opened for reading. The position is reset accordingly to the beginning of the file.

It is read into the field NUM. For information about the READ DATASET statement, refer to Reading Data from Files. The values of NUM are displayed on the screen.

It is reopened for writing The position is reset to the beginning of the file.

It is filled with five integers, which overwrite the previous contents of the file.

It is then reopened for reading. The position is reset to the beginning of the file.

The file is read into the field NUM. The values of NUM are displayed on the screen.

It is closed (for information about the CLOSE DATASET statement, refer to Closing a File).

It is then reopened for writing. The system deletes the existing contents of the file.

It is filled with 5 integers.

It is then opened for reading. The position is reset to the beginning of the file.

The file is read into the field NUM. The values of NUM are displayed on the screen.

open a file in text mode, use the IN TEXT MODE addition to the OPEN DATASET statement.

Syntax

OPEN DATASET <dsn> FOR .... IN TEXT MODE.

If you read from or write to a file that is open in text mode, the data is transferred line by line. The system assumes that the file has a line structure.

In each TRANSFER statement, the system transfers all bytes (apart from spaces at the end) into the file, and places an end of line marker at the end. For information about the TRANSFER statement, refer to Writing Data to Files.

In each READ DATASET statement, the system reads all of the data up to the next end of line marker. For information about the READ DATASET statement, refer to Reading Data from Files. If the target field is too small, the line is truncated. If it is longer than the line in the file, it is filled with trailing spaces.

You should always use text mode if you want to write strings to files or where you know that an existing file has a line construction. You can, for example, use text mode to read files that you have created using any editor on your application server.

The following example works in R/3 Systems that are running on UNIX systems using ASCII.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT(4),

HEX TYPE X.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE.

TRANSFER '12 ' TO FNAME.

TRANSFER '123456 9 ' TO FNAME.

TRANSFER '1234 ' TO FNAME.

OPEN DATASET FNAME FOR INPUT IN TEXT MODE.

READ DATASET FNAME INTO TEXT.

WRITE / TEXT.

READ DATASET FNAME INTO TEXT.

WRITE TEXT.

READ DATASET FNAME INTO TEXT.

WRITE TEXT.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

SKIP.

DO.

READ DATASET FNAME INTO HEX.

If SY-SUBRC <> 0.

EXIT.

ENDIF.

WRITE HEX.

ENDDO.

The output appears as follows:

12 1234 1234

31 32 0A 31 32 33 34 35 36 20 20 39 0A 31 32 33 34 0A

This example opens a file "myfile" for writing in text mode. Three literals with length 10 characters are written to it. After the file has been opened for reading in text mode, the lines are read into the field TEXT (length 4). The first line is filled with two trailing spaces. The last five characters of the second line are truncated. The structure of the file is displayed by opening it in binary mode and reading its contents into the hexadecimal field HEX. The numbers 31 - 36 are the ASCII codes for the digits 1 - 6. 20 is the code for the space character. The end of each line is marked by 0A. Note that any spaces at the end of strings are not written to the file.

Reading Data from Files

To read data from a file on the application server, use the READ DATASET statement:

Syntax

READ DATASET <dsn> INTO <f> [LENGTH <len>].

This statement reads data from the file <dsn> into the variable <f>. In order to determine into which variable you should read data from a file, you need to know the structure of the file.

You can specify the transfer mode in the OPEN DATASET statement. If you have not already opened the file for reading, the system tries to open it either in binary mode, or using the additions from the last OPEN DATASET statement. However, it is good practice only to open files using the OPEN DATASET statement. For further information about the OPEN DATASET statement and the naming conventions for files, refer to Opening a File.

If the system was able to read data successfully, SY-SUBRC is set to 0. When the end of the file is reached, SY-SUBRC is set to 4. If the file could not be opened, SY-SUBRC is set to 8.

If you are working in binary mode, you can use the LENGTH addition to find out the length of the data transferred to <f>. The system sets the value of the variable <len> to this length.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(12) VALUE 'abcdefghijkl',

TEXT2(5),

LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN BINARY MODE.

TRANSFER TEXT1 TO FNAME.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN BINARY MODE.

DO.

READ DATASET FNAME INTO TEXT2 LENGTH LENG.

WRITE: / SY-SUBRC, TEXT2, LENG.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

CLOSE DATASET FNAME.

The output is:

0 abcde 5

0 fghij 5

4 kl### 2

This example fills the file "myfile" with 12 bytes from the field TEXT1. It is then read into the field TEXT2 in 5-byte portions. Note here that the system fills up the last three bytes of TEXT2 with zeros after the end of the file has been reached. The number of bytes transferred is contained in the field LENG.

If you are working in text mode, you can use the LENGTH addition to find out the length of the current line in the file. The system sets the value of the variable <len> to the length of the line. The system calculates this by counting the number of bytes between the current position and the next end of line marker in the file.

DATA FNAME(60) VALUE 'myfile'.

DATA: TEXT1(4) VALUE '1234 ',

TEXT2(8) VALUE '12345678',

TEXT3(2),

LENG TYPE I.

OPEN DATASET FNAME FOR OUTPUT IN TEXT MODE.

TRANSFER: TEXT1 TO FNAME,

TEXT2 TO FNAME.

CLOSE DATASET FNAME.

OPEN DATASET FNAME FOR INPUT IN TEXT MODE.

DO 2 TIMES.

READ DATASET FNAME INTO TEXT3 LENGTH LENG.

WRITE: / TEXT3, LENG.

ENDDO.

CLOSE DATASET FNAME.

The output appears as follows:

12 4

12 8

This example writes the strings TEXT1 and TEXT2 to the file "myfile" in text mode. They are then read into the string TEXT3 (length 2). The amount of memory occupied by the lines is read into the field LENG.