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

upload file to SAP Application Server

Former Member
0 Likes
3,169

Hi all ,

I need to upload an internal table to SAP Application Server, and the file name is 'XXX.csv'. Can anyone please help me to Solve this Issue?

Thanks in Advance!

15 REPLIES 15
Read only

former_member188829
Active Contributor
0 Likes
2,107

use open dataset,

transfer and close dataset statements

Read only

0 Likes
2,107

use transaction<b> CG3Z</b> for same.

Read only

Former Member
0 Likes
2,107

Hi Nina,

Use the following Syntax:

Declarations:

p_binary --To download the file in binary format else text format.

p_file_path TYPE STRING -- contains the file path where the file needs to be downloaded on application server

f_messages--Contains Error Messages

itab_data--Internal table containing the data to be downloaded

<fs_wa>--Field Symbol

f_line TYPE string--Used to transfer the file line by line.


      IF p_binary IS INITIAL.
        OPEN DATASET p_file_path FOR OUTPUT MESSAGE f_messages
                                 IN TEXT MODE.
      ELSEIF p_binary = 'X'.
        OPEN DATASET p_file_path FOR OUTPUT MESSAGE f_messages
                                 IN BINARY MODE.
      ENDIF.

      IF sy-subrc <> 0.
        EXIT.
      ENDIF.

      LOOP AT itab_data ASSIGNING <fs_wa>.

        DO.

          ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_any>.
          IF sy-subrc <> 0.
            EXIT.
          ENDIF.

          IF NOT f_line IS INITIAL.
       CONCATENATE f_line <fs_any> INTO f_line SEPARATED BY p_separator.
          ELSE.
            f_line = <fs_any>.
          ENDIF.

        ENDDO.

        TRANSFER f_line TO p_file_path.
        CLEAR f_line.
      ENDLOOP.

      CLOSE DATASET p_file_path.

    ENDIF.

  ENDIF.

Regards,

Chetan.

PS:Reward points if this helps.

Read only

0 Likes
2,107

Hi Chetan H,

Good morning!

I can't clearly understand the following code in your code:

-


LOOP AT itab_data ASSIGNING <fs_wa>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_any>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

IF NOT f_line IS INITIAL.

CONCATENATE f_line <fs_any> INTO f_line SEPARATED BY p_separator.

ELSE.

f_line = <fs_any>.

ENDIF.

ENDDO.

TRANSFER f_line TO p_file_path.

CLEAR f_line.

ENDLOOP.

-


My code is:

--

LOOP AT itab_data ASSIGNING <fs_wa>.

TRANSFER <fs_wa> TO p_file_path.

ENDLOOP.

-


Can you tell me why you use <fs_any> and f_line and why you need to judge the f_line whether is initial?

Thanks a lot?

--

Read only

Former Member
0 Likes
2,107

Use demo code as below n reward points if helpfull -

1) use transaction CG3Z

2) or demo code - (save ur file as TExt Tab)

&----


*& Report ZGILL_AS *

*& *

&----


*& *

*& *

&----


REPORT ZGILL_AS message-id rp .

tables: pa0001,pa0002.

select-options s_pernr for pa0001-pernr no intervals MODIF ID XYZ.

parameters: p_dwnld AS CHECKBOX ,

p_upld AS CHECKBOX DEFAULT 'X'.

parameters: P_DSNI(75) TYPE C MODIF ID ABG DEFAULT

'/usr/local/sapdata/amit.dat' LOWER CASE.

data: begin of itab occurs 0,

pernr(8),

sp1(1) value ',',

werks(4),

sp2(1) value ',',

persg(1),

sp3(1) value ',',

persk(2),

end of itab.

data: s_eof(3).

start-of-selection.

if p_upld = 'X'.

OPEN DATASET P_DSNI FOR OUTPUT IN LEGACY TEXT MODE.

PERFORM FETCH_DATA.

STOP.

elseif p_dwnld = 'X'.

OPEN DATASET P_DSNI FOR INPUT IN LEGACY TEXT MODE.

IF SY-SUBRC NE 0.

MESSAGE E016 WITH

'Error opening seq. file, RC:' SY-SUBRC.

EXIT.

ENDIF.

CLEAR S_EOF.

DO.

PERFORM FETCH_file.

IF S_EOF EQ 'YES'. stop. ENDIF.

ENDDO.

endif.

END-OF-SELECTION.

if itab[] is not initial.

perform print_file1 tables itab.

else.

write:/ 'No records exists'.

endif.

&----


*& Form FETCH_DATA

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM FETCH_DATA .

SELECT * FROM PA0001 WHERE PERNR IN S_PERNR.

MOVE-CORRESPONDING PA0001 TO ITAB.

TRANSFER ITAB TO P_DSNI.

APPEND ITAB.

ENDSELECT.

CLOSE DATASET P_DSNI.

ENDFORM. " FETCH_DATA

&----


*& Form FETCH_file

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM FETCH_file .

READ DATASET P_DSNI INTO itab.

append itab.

clear itab.

IF SY-SUBRC NE 0.

S_EOF = 'YES'. EXIT.

ENDIF.

ENDFORM. " FETCH_file

&----


*& Form print_file1

&----


  • text

----


  • -->P_ITAB text

----


FORM print_file1 tables P_ITAB structure itab .

write:/2 'EmpNo',

14 'Personnel Area',

34 'Emp Group',

47 'Emp SubGroup'.

skip 1.

loop at p_itab.

write:2 p_itab-pernr,

14 p_itab-werks,

34 p_itab-persg,

47 p_itab-persk.

skip 1.

endloop.

ENDFORM. " print_file1

Message was edited by:

Amit Tyagi

Read only

Former Member
0 Likes
2,107

Hi Nina,

Try this code:

<b>OPEN DATASET</b> FILENAME <b>FOR OUTPUT IN TEXT MODE</b> .

IF SY-SUBRC <> 0.

MESSAGE 'Error in opening file'.

ELSE.

LOOP AT Internal table into work_area.

TRANSFER work_area TO FILENAME.

ENDLOOP.

<b>CLOSE DATASET </b>FILENAME.

Hope this helps,

Pragya

Read only

Former Member
0 Likes
2,107

Below sample code might give you some idea:

data: it_t001 type table of t001,
      wa_t001 type t001.
data: fname type filename value '/usr/sap/test.csv'.

field-symbols: <wa>, <fld>.
data: str type string.

select * into table it_t001 from t001.

open dataset fname for output in text mode encoding default.
if sy-subrc ne 0.
   write:/ 'Unable to open file:', fname.
else.
   loop at it_t001 assigning <wa>.
        clear str.
        do.
          assign component sy-index of structure <wa> to <fld>.
          if sy-subrc ne 0.
             exit.
          elseif sy-index = 1.
             move <fld> to str.
          else.
             concatenate str <fld> into str separated by ','.
          endif.
        enddo.
        transfer str to fname.
   endloop.
   close dataset fname.
endif.

Regards

Eswar

Read only

0 Likes
2,107

Hi Eswar,

When I use the sample code, the result is "Unable to open file" . How can i open this file?

Thank you a lot

Read only

0 Likes
2,107

Hi Nina,

data: fname type filename value '/usr/sap/test.csv'.

The directory '/usr/sap' should be available in your system you can check this in AL11 transaction.Check in this transaction which directory is available in your system.Use that at this place for the file path declaration.

Mukesh Kumar

Read only

0 Likes
2,107

Hi Nina

You have to change the path of the file name:

data: fname type filename value '<b>/usr/sap/</b>test.csv'.

Specify a directory on your application server.

Regards

Eswar

Read only

0 Likes
2,107

You can try with this path

/usr/sap/trans/test.csv or

/tmp

goto AL11 and u check out the paths

Read only

0 Likes
2,107

Hello all,

My code is like follows:

There is no error, but I can't find the a.csv file in the "\usr\sap\tmp". Can you tell me why?

Thank you a lot!

-


TYPES:

ts_sflight TYPE sflight,

tt_sflight TYPE STANDARD TABLE OF ts_sflight.

DATA:

gs_sflight TYPE ts_sflight,

gt_sflight TYPE tt_sflight.

PARAMETERS pv_file(30) TYPE c

DEFAULT '\usr\sap\tmp\a.csv'.

select * into table gt_sflight from sflight.

OPEN DATASET pv_file FOR OUTPUT IN BINARY MODE.

if sy-subrc ne 0.

Write: 'Unable to open the specifield application server file', pv_file.

stop.

endif.

LOOP AT gt_sflight INTO gs_sflight.

TRANSFER gs_sflight TO pv_file.

ENDLOOP.

Read only

0 Likes
2,107

1. Check out if ur internal table is empty

2.Debug and check for sy-subrc after open dataset

3.change the path to /tmp/a.csv and try

Read only

0 Likes
2,107

Hi Nina

As far as i know the separator for directories in UNIX environment is "/"-> Forward Slash. Please check.

Regards

Eswar

Read only

Former Member
0 Likes
2,107

Hi,

The tcodes CG3y, CG3Z to upload presentation file to application server file and download same.

I am also providing code for that.


data: ls_outfile type char2000,
         lt_infile type table of char2000.

* Selection screen
parameters: p_input  type char255 obligatory.
             p_output type char255 obligatory.
          
start-of-selection.
  open dataset p_output for output in text mode.
  if sy-subrc ne 0.
    Write 'Unable to open the specifield application server file', p_output.
    stop.
 Endif.

  call function 'WS_UPLOAD'
       exporting
            filename            = p_input
            filetype            = 'ASC'
       tables 
            data_tab          = lt_infile
       exceptions
            conversion_error    = 01
            file_open_error     = 02
            file_read_error     = 03
            invalid_table_width = 04
            invalid_type        = 05
            no_batch            = 06
            unknown_error       = 07.

  if syst-subrc NE 0.
    write: /'Unable to upload', p_input.
    stop.
  endif.

  loop at lt_infile into ls_outfile.
    transfer ls_outfile to p_output.
  endloop.

  describe table lt_infile lines lv_lines.
  write: / lv_lines, 'records transfered.'.

  close dataset p_output.

Regards

Bhupal Reddy