download file to sap application server using...

25
Download File to SAP Application Server using ABAP Use the following steps to download the ABAP internal table data to a file in SAP application server. 1. Declare a ABAP internal table and fill the internal table with required data. 2. Use OPEN DATASET ABAP statement to open/create a file on the SAP application server. 3. Loop through the internal table and use TRANSFER ABAP statement to move each internal table record to file on application server. 4. Close the file on the application server using CLOSE DATASET ABAP statement. Below program uses OPEN DATASET, TRANSFER and CLOSE DATASET statements to download the file. *----------------------------------------------------------------------* * Data Decalaration *----------------------------------------------------------------------* DATA: gt_spfli TYPE TABLE OF spfli, gwa_spfli TYPE spfli. DATA: gv_file TYPE rlgrap-filename. *----------------------------------------------------------------------* * START-OF-SELECTION *----------------------------------------------------------------------* PERFORM get_data. IF NOT gt_spfli[] IS INITIAL. PERFORM save_file. ELSE. MESSAGE 'No data found' TYPE 'I'. ENDIF. *&---------------------------------------------------------------------* *& Form get_data *&---------------------------------------------------------------------* FORM get_data.

Upload: others

Post on 09-Aug-2021

41 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Download File to SAP Application Server using ABAP

Use the following steps to download the ABAP internal table data to a file in SAP application server.

1. Declare a ABAP internal table and fill the internal table with required data.

2. Use OPEN DATASET ABAP statement to open/create a file on the SAP application server.

3. Loop through the internal table and use TRANSFER ABAP statement to move each internal table record to

file on application server.

4. Close the file on the application server using CLOSE DATASET ABAP statement.

Below program uses OPEN DATASET, TRANSFER and CLOSE DATASET statements to download the file.

*----------------------------------------------------------------------*

* Data Decalaration

*----------------------------------------------------------------------*

DATA: gt_spfli TYPE TABLE OF spfli,

gwa_spfli TYPE spfli.

DATA: gv_file TYPE rlgrap-filename.

*----------------------------------------------------------------------*

* START-OF-SELECTION

*----------------------------------------------------------------------*

PERFORM get_data.

IF NOT gt_spfli[] IS INITIAL.

PERFORM save_file.

ELSE.

MESSAGE 'No data found' TYPE 'I'.

ENDIF.

*&---------------------------------------------------------------------*

*& Form get_data

*&---------------------------------------------------------------------*

FORM get_data.

Page 2: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

*Get data from table SPFLI

SELECT * FROM spfli

INTO TABLE gt_spfli.

ENDFORM. " get_data

*&---------------------------------------------------------------------*

*& Form save_file

*&---------------------------------------------------------------------*

FORM save_file.

DATA: lv_data TYPE string.

*Move complete path to filename

gv_file = 'spfli.txt'.

* Open the file in output mode

OPEN DATASET gv_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

MESSAGE 'Unable to create file' TYPE 'I'.

EXIT.

ENDIF.

LOOP AT gt_spfli INTO gwa_spfli.

CONCATENATE gwa_spfli-carrid

gwa_spfli-connid

gwa_spfli-countryfr

gwa_spfli-cityfrom

Page 3: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

gwa_spfli-airpfrom

gwa_spfli-countryto

gwa_spfli-cityto

gwa_spfli-airpto

gwa_spfli-arrtime

INTO lv_data

SEPARATED BY ','.

*TRANSFER moves the above fields from workarea to file with comma

*delimited format

TRANSFER lv_data TO gv_file.

CLEAR: gwa_spfli.

ENDLOOP.

* close the file

CLOSE DATASET gv_file.

ENDFORM. " save_file

When you execute the above program, the data in the internal table will be downloaded to a file on the application

server. Use t-code AL11 to view the file on SAP application server.

Just double click on the file “spfli.txt” to view the contents.

Page 4: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click
Page 5: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Upload File from SAP Application Server using ABAP

Use the following steps to upload data from a file in SAP application server to ABAP internal table.

1. Declare a ABAP internal table.

2. Use OPEN DATASET ABAP statement to open file on appliction server.

3. Use READ DATASET ABAP statement to read each line in the file to workarea. Append work area data to

internal table.

4. Use CLOSE DATASET ABAP statement to close the application server file.

5. Process the data in the internal table.

Below program uses OPEN DATASET, READ DATASET and CLOSE DATASET statements to upload the file.

*----------------------------------------------------------------------*

* Data Decalaration

*----------------------------------------------------------------------*

DATA: gt_spfli TYPE TABLE OF spfli,

gwa_spfli TYPE spfli.

DATA: gv_file TYPE rlgrap-filename.

*----------------------------------------------------------------------*

* START-OF-SELECTION

*----------------------------------------------------------------------*

PERFORM read_file.

PERFORM display_data.

*&---------------------------------------------------------------------*

*& Form read_file

*&---------------------------------------------------------------------*

FORM read_file.

DATA: lv_data TYPE string.

Page 6: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

*Move complete path to filename

gv_file = 'spfli.txt'.

*Open the file in application server to read the data

OPEN DATASET gv_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

MESSAGE 'Unable to open file' TYPE 'I'.

ENDIF.

DO.

* Loop through the file, if a record is found move it

* to temporary structure else exit out of the loop.

READ DATASET gv_file INTO lv_data.

IF sy-subrc = 0.

* Split the fields in temporary structure to corresponding

* fields in workarea.

SPLIT lv_data AT ',' INTO gwa_spfli-carrid

gwa_spfli-connid

gwa_spfli-countryfr

gwa_spfli-cityfrom

gwa_spfli-airpfrom

gwa_spfli-countryto

gwa_spfli-cityto

gwa_spfli-airpto

gwa_spfli-arrtime.

APPEND gwa_spfli TO gt_spfli.

Page 7: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

CLEAR gwa_spfli.

ELSE.

EXIT.

ENDIF.

ENDDO.

*Close the file

CLOSE DATASET gv_file.

ENDFORM. " read_file

*&---------------------------------------------------------------------*

*& Form DISPLAY_DATA

*&---------------------------------------------------------------------*

FORM display_data .

LOOP AT gt_spfli INTO gwa_spfli.

WRITE:/ gwa_spfli-carrid,

gwa_spfli-connid,

gwa_spfli-countryfr,

gwa_spfli-cityfrom,

gwa_spfli-airpfrom,

gwa_spfli-countryto,

gwa_spfli-cityto,

gwa_spfli-airpto,

gwa_spfli-arrtime.

CLEAR: gwa_spfli.

Page 8: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

ENDLOOP.

ENDFORM. " DISPLAY_DATA

When you execute the above program reads the data from the file in application server and displays the following

output.

Page 9: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Upload File from SAP Presentation Server using ABAP

Use the following steps to upload data from a file in SAP presentation server to ABAP internal table.

1. Declare a ABAP internal table.

2. Use function module ‘GUI_UPLOAD’ or ‘GUI_UPLOAD’ method of ‘CL_GUI_FRONTEND_SERVICES’ class

to upload the data to ABAP internal table.

3. Process the data in the internal table.

4. Below program uses the function module GUI_UPLOAD to upload the file.

5. *----------------------------------------------------------------------*

6. * Data Decalaration

7. *----------------------------------------------------------------------*

8. DATA: gt_spfli TYPE TABLE OF spfli,

9. gwa_spfli TYPE spfli.

10.

11. DATA: gv_filename TYPE string,

12. gv_filetype TYPE char10.

13. *----------------------------------------------------------------------*

14. * START-OF-SELECTION

15. *----------------------------------------------------------------------*

16. PERFORM read_file.

17. PERFORM process_data.

18.

19. *&---------------------------------------------------------------------*

20. *& Form read_file

21. *&---------------------------------------------------------------------*

22. FORM read_file.

23. *Move complete file path to file name

24. gv_filename = 'C:\test\data.txt'.

25.

26. *Upload the data from a file in SAP presentation server to internal

27. *table

28. CALL FUNCTION 'GUI_UPLOAD'

29. EXPORTING

30. filename = gv_filename

31. filetype = 'ASC'

32. has_field_separator = 'X'

33. TABLES

34. data_tab = gt_spfli.

35.

36. ENDFORM. " read_file

37.

38. *&---------------------------------------------------------------------*

39. *& Form process_data

Page 10: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

40. *&---------------------------------------------------------------------*

41. FORM process_data.

42.

43. *Display the internal table data

44. LOOP AT gt_spfli INTO gwa_spfli.

45. WRITE:/ gwa_spfli-carrid,

46. gwa_spfli-connid,

47. gwa_spfli-countryfr,

48. gwa_spfli-cityfrom,

49. gwa_spfli-airpfrom,

50. gwa_spfli-countryto,

51. gwa_spfli-cityto,

52. gwa_spfli-airpto,

53. gwa_spfli-arrtime.

54. CLEAR: gwa_spfli.

55. ENDLOOP.

56. ENDFORM. " process_data

57. When you execute the above program reads the data from the file in presentation server and displays the

following output.

Page 11: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

58.

59. You can also use GUI_UPLOAD method of CL_GUI_FRONTEND_SERVICES class to upload file from

presentation server. Instead of calling GUI_UPLOAD function module in the read_file subroutine of above

program, call the GUI_UPLOAD method of **CL_GUI_FRONTEND_SERVICES class.

60. CALL METHOD cl_gui_frontend_services=>gui_upload

61. EXPORTING

62. filename = gv_filename

63. filetype = 'ASC'

64. has_field_separator = 'X'

65. CHANGING

66. data_tab = gt_spfli.

Page 12: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Downloading files onto the Application Server

By Vikram Chellappa, Mouri Tech Solutions

Description

This is a demo application to download the data from the internal table of the application to a file in theapplication server.

Step by Step Guidance.

Go to transaction SE38 to open ABAP editor and Type in the program name click the create button. Herewe have used “ZDOWNLOAD_APPL_DEMO” as the program name.

Enter the Short Description for the program

Select the Program type to be “Executable program”

Page 13: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Click on SAVE CHECK and ACTIVATE.

Page 14: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Give the path of the file which has to be uploaded

Here we have the file in location “\USR\SAP\SRI\SYS\SRC\DOWN.TXT”

The drive name is taken as the default SAP installation drive.

OUTPUT LIST SCREEN

The data from the internal table is moved in to DOWN.TXT file in the Presentation server and out putdisplayed on the list screen shows the data in the internal table.

Output File in the Application server.

Go to Transaction AL11 to see the SAP installation directories

Find our directory double click

Page 15: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Find our downloaded file in the directory and double click it

Page 16: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

The Downloaded data is displayed

SOURCE CODEREPORT ZDOWNLOAD_APPL_DEMO.TYPES : BEGIN OF ST_DEMO, REG_NO(10) TYPE C, NAME(20) TYPE C, ADDR(20) TYPE C, END OF ST_DEMO.DATA : WA_DEMO TYPE ST_DEMO, IT_DEMO TYPE TABLE OF ST_DEMO, L_FNAME TYPE STRING .PARAMETERS: P_FNAME(128) TYPE C DEFAULT '\usr\sap\SRI\SYS\src\DOWN.TXT'OBLIGATORY.L_FNAME = P_FNAME.WA_DEMO-REG_NO = '100001'.WA_DEMO-NAME = 'ANAND'.

Page 17: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

WA_DEMO-ADDR = 'NAGARKOVIL'. APPEND WA_DEMO TO IT_DEMO.WA_DEMO-REG_NO = '100002'.WA_DEMO-NAME = 'VIKRAM'.WA_DEMO-ADDR = 'CHENNAI'.APPEND WA_DEMO TO IT_DEMO. OPEN DATASET L_FNAME FOR OUTPUT IN TEXT MODE ENCODING DEFAULT. WRITE :5 'REG NUM',16 'NAME',37 'ADDRESS' . LOOP AT IT_DEMO INTO WA_DEMO. IF SY-SUBRC = 0. TRANSFER WA_DEMO TO L_FNAME. WRITE :/5 WA_DEMO-REG_NO,16 WA_DEMO-NAME,37 WA_DEMO-ADDR. ENDIF. ENDLOOP.

Page 18: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Upload File from SAP Presentation Server using ABAP

Use the following steps to upload data from a file in SAP presentation server to ABAP internal table.

1. Declare a ABAP internal table.

2. Use function module ‘GUI_UPLOAD’ or ‘GUI_UPLOAD’ method of ‘CL_GUI_FRONTEND_SERVICES’ class

to upload the data to ABAP internal table.

3. Process the data in the internal table.

Below program uses the function module GUI_UPLOAD to upload the file.

*----------------------------------------------------------------------*

* Data Decalaration

*----------------------------------------------------------------------*

DATA: gt_spfli TYPE TABLE OF spfli,

gwa_spfli TYPE spfli.

DATA: gv_filename TYPE string,

gv_filetype TYPE char10.

*----------------------------------------------------------------------*

* START-OF-SELECTION

*----------------------------------------------------------------------*

PERFORM read_file.

PERFORM process_data.

*&---------------------------------------------------------------------*

*& Form read_file

*&---------------------------------------------------------------------*

FORM read_file.

*Move complete file path to file name

Page 19: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

gv_filename = 'C:\test\data.txt'.

*Upload the data from a file in SAP presentation server to internal

*table

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = gv_filename

filetype = 'ASC'

has_field_separator = 'X'

TABLES

data_tab = gt_spfli.

ENDFORM. " read_file

*&---------------------------------------------------------------------*

*& Form process_data

*&---------------------------------------------------------------------*

FORM process_data.

*Display the internal table data

LOOP AT gt_spfli INTO gwa_spfli.

WRITE:/ gwa_spfli-carrid,

gwa_spfli-connid,

gwa_spfli-countryfr,

gwa_spfli-cityfrom,

Page 20: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

gwa_spfli-airpfrom,

gwa_spfli-countryto,

gwa_spfli-cityto,

gwa_spfli-airpto,

gwa_spfli-arrtime.

CLEAR: gwa_spfli.

ENDLOOP.

ENDFORM. " process_data

When you execute the above program reads the data from the file in presentation server and displays the following

output.

Page 21: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

You can also use GUI_UPLOAD method of CL_GUI_FRONTEND_SERVICES class to upload file from presentation

server. Instead of calling GUI_UPLOAD function module in the read_file subroutine of above program, call the

GUI_UPLOAD method of **CL_GUI_FRONTEND_SERVICES class.

CALL METHOD cl_gui_frontend_services=>gui_upload

EXPORTING

filename = gv_filename

filetype = 'ASC'

has_field_separator = 'X'

CHANGING

data_tab = gt_spfli.

Page 22: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click
Page 23: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

Download File to SAP Presentation Server using ABAP

Use the following steps to download the ABAP internal table data to a file in SAP application server.

1. Declare a ABAP internal table.

2. Fill the internal table with required data.

3. Use function module GUI_DOWNLOAD’ or ‘GUI_DOWNLOAD’ method of

‘CL_GUI_FRONTEND_SERVICES’ class to download the data.

Below program uses function module GUI_DOWNLOAD to download the file.

*----------------------------------------------------------------------*

* Data Decalaration

*----------------------------------------------------------------------*

DATA: gt_spfli TYPE TABLE OF spfli,

gwa_spfli TYPE spfli.

DATA: gv_filename TYPE string,

gv_filetype TYPE char10.

*----------------------------------------------------------------------*

* START-OF-SELECTION

*----------------------------------------------------------------------*

PERFORM get_data.

IF NOT gt_spfli[] IS INITIAL.

PERFORM save_file.

ELSE.

MESSAGE 'No data found' TYPE 'I'.

ENDIF.

*&---------------------------------------------------------------------*

*& Form get_data

Page 24: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

*&---------------------------------------------------------------------*

FORM get_data.

*Get data from table SPFLI

SELECT * FROM spfli

INTO TABLE gt_spfli.

ENDFORM. " get_data

*&---------------------------------------------------------------------*

*& Form save_file

*&---------------------------------------------------------------------*

FORM save_file.

*Move complete file path to file name

gv_filename = 'C:\test\data.txt'.

*Download the internal table data into a file in SAP presentation server

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = gv_filename

filetype = 'ASC'

write_field_separator = 'X'

TABLES

data_tab = gt_spfli.

ENDFORM. " save_file

When you execute the above program, the file ‘data.txt’ will be downloaded to ‘C:\test’.

Page 25: Download File to SAP Application Server using ABAPdocshare04.docshare.tips/files/30451/304510311.pdf · Use t-code AL11 to view the file on SAP application server. Just double click

We can also use GUI_DOWNLOAD method of CL_GUI_FRONTEND_SERVICES class to download the file. Instead

of calling GUI_DOWNLOAD function module in the save_file subroutine of above program, call the

GUI_DOWNLOAD method of CL_GUI_FRONTEND_SERVICES class.

CALL METHOD cl_gui_frontend_services=>gui_download

EXPORTING

filename = gv_filename

filetype = 'ASC'

write_field_separator = 'X'

CHANGING

data_tab = gt_spfli.